0

Will you please please tell me how to get java variable "data_id" value in PHP Variable "$gal_id" i m not able pass the value of java variable into PHP Variable so please also let me know if you find the solution

JS IN Head

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/jquery.colorbox.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//Examples of how to assign the Colorbox event to elements

$(".inline").colorbox({inline:true, width:"70%", height:"500px;"});
});
</script>

HTML

<li><a class='inline' href="#inline_content" data-id="1">ABC</a></li>
<li><a class='inline' href="#inline_content" data-id="2">DEF</a></li>
<li><a class='inline' href="#inline_content" data-id="3">XYZ</a></li>

POPUP Box

<script type="text/javascript">
$('.inline').click(function(){
   var data_id = $(this).data('id');
   $('#idvalue').html(""+data_id);
});
</script>

<div style='display:none'>
    <div id='inline_content'>
    <span id="idvalue"></span><!--we'll get here data_id value of clicked list in output -->
    <?php
    $gal_id = ""; // Will you please please tell me how to get java variable "data_id" value here from above js
    $check = mysql_query("select * from img where id = '$gal_id'");
    while ($run = mysql_fetch_array($check)){

    .
    .
    .
    }?>

</div>
</div>
EdChum
  • 376,765
  • 198
  • 813
  • 562
SPS
  • 116
  • 1
  • 11
  • you need to use `ajax`... – Nishant Solanki Mar 10 '15 at 11:14
  • php is a server side language so it will be executed when the page is loaded.. so you cant pass js variable in php... – Nishant Solanki Mar 10 '15 at 11:14
  • hmmm...i m new on php and also i m not familiar with ajax.. so please tell me if there is another way to do this or with help of jquery etc – SPS Mar 10 '15 at 11:16
  • not possible without ajax.. but you can still do one thing.. you can fetch all the images when page is loaded, and than show them with `if..else if`... in your popup box – Nishant Solanki Mar 10 '15 at 11:18
  • its too long process to do...page load time increase due to this. – SPS Mar 10 '15 at 11:21
  • You should google about AJAX call. Its not a good practice to load all of the images once page is loaded. It will slow your page speed if your website will get more content / images to display. – Vineet Mar 10 '15 at 11:21
  • @Samar yes i know.. but it was a quick resolution.. or else you will need to use ajax... and it is easy to implement – Nishant Solanki Mar 10 '15 at 11:22
  • will you please provide any refrence website from where i can get refrence about ajax call. – SPS Mar 10 '15 at 11:26

2 Answers2

0

If I understood your question, you can't do it this way. You can check this other question that can surely help you: How to pass JavaScript variables to PHP?

Community
  • 1
  • 1
lyisia
  • 144
  • 8
0

try this edit... I have integrated ajax with your code

<script type="text/javascript">
$('.inline').click(function(){
   var data_id = $(this).data('id');
   $('#idvalue').html(""+data_id);
 $.ajax({
         type: "GET",
         url: "ajax.php?gal_id="+data_id,
         success: function(data) {
             $('#images').html(data);
         }
     }); 
});
</script>

<div style='display:none'>
    <div id='inline_content'>
    <span id="idvalue"></span>
<span id="images"></span>


</div>
</div>

create a new file ajax.php

and put this code there...

<?php
//whatever content you will echo here will be send to the main page and put to #images span tag...
//put your connection string here.. to connect the database
        $gal_id = $_GET['gal_id']; 
        $check = mysql_query("select * from img where id = '$gal_id'");
        while ($run = mysql_fetch_array($check)){

        .
        .
        .
        }?>
Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32