Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

HTML (When this DIV is clicked a call to searchfunction made and its id is passed)

<div class="me" id="<?php echo $row['main_cat_id'];?>" onClick="searchfunction('<?php echo $row['main_cat_id'];?>');">
    <?php echo $row[ 'main_cat_name'];?>
</div>

Below is the search function; it passes the ID value of the clicked DIV to the pagegetparam.php.

 function searchfunction(param) {
    //alert(param);
    var xmlhttp = getXmlHttp();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (this.responseText !== null) {
                var ajaxElm = document.getElementById('getparam');
                ajaxElm.innerHTML = this.responseText; // append in front
                //jQuery(ajaxElm).append(this.responseText);
            }
        }
    }
    xmlhttp.open("GET", "getparam.php?param=" + param, true);
    xmlhttp.send();
}

And getparam.php only passes back the ID to the same page it got it from. I've resorted to this method, because I believe there's no way to directly pass the id of the clicked div which was captured by JavaScript and assign it to a php variable.

<?php
$param=$_GET['param'];
echo $param;
?>

Now, I need the value of clicked div id to run this query below:

echo $param='<span id="getparam"></span>';//it does echo the id in numeric like, 1 or 2 .But it doesn't help to execute the query though.

 $sql= "SELECT * FROM categories,main_category WHERE categories.main_cat_id=main_category.main_cat_id AND categories.main_cat_id='$param'";

How can I get the clicked DIV id passed to the php variable $param? Is there any other way or can anything be improved here?

share|improve this question
1  
I looked at that code for several minutes and i still have no idea what you are doing. Why don't you simply put the query into a file, redirect the AJAX to it and then just $sql = "SELECT * ... AND categories.main_cat_id='".$param."'";? – Y U NO WORK Jun 10 at 6:18

2 Answers 2

Javascript is client side and PHP has the server side. You can store the value of a php variable to a variable of JAVASCRIPT but you can't do the reverse.

In order to save the value of javascript, you would need to make an ajax call to the server side to store the value in php.

share|improve this answer

I can't unstand you very clearly. Do you prepare to get the js function searchfunction(param) into your SQL sentence in PHP code? if so you may just try $sql= "SELECT * FROM categories,main_category WHERE categories.main_cat_id=main_category.main_cat_id AND categories.main_cat_id='{$param}'";

share|improve this answer
    
I tried it before, it doesn't work – Keren Jun 10 at 6:37
    
so where $param='<span id="getparam"></span>'; shows? can you show your whole code or tell me your purpose? – Gary Liu - MSFT Jun 10 at 6:54
    
I already placed the code above. The problem now is, $param='<span id="getparam"></span>'; is not echoing inside the query. When I simply echo $param='<span id="getparam"></span>'; it does show the ID of clicked item. I expect the veluw to be reflected inside the query too but it doesnt – Keren Jun 10 at 7:03
    
Now the most important is that I can't understand that is there any relationship between the first 2/3 part before $param='<span id="getparam"></span>'; and the rest. You just create a HTML DOM, and you want get the div's id as a query param in SQL? if so which file the SQL sentence located in? – Gary Liu - MSFT Jun 10 at 7:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.