user1109631
user1109631

Reputation: 21

how to access javascript variable with php code

        function al(){
        var selr = jQuery('#grid').jqGrid('getGridParam','selrow');
        if(selr){
            <?
                include_once("../../DB/singleton.php");
                $pDatabase = Database::getInstance();
                $c = $pDatabase->query("select c.city_title as 'City' from teamshuffle.tbl_city c, teamshuffle.tbl_sport_city s where c.tblcity_id = s.tblcity_id and s.tblsport_id = " . );
                while($r = mysql_fetch_array($c)){
                    echo("alert(\"" . $r[0] . selr . "\");");
                }
            ?>

        }
    }

This is my javascript function. I need to access variable "selr" in the line

echo("alert(\"" . $r[0] . $d . "\");");

Upvotes: 2

Views: 844

Answers (5)

ted
ted

Reputation: 5329

Sorry, haven't tested it, just an guess though. Try using cookies;

function al(){
    var selr = jQuery('#grid').jqGrid('getGridParam','selrow');
    setCookie("selr", selr, 1);

    if(selr){
        <?
            include_once("../../DB/singleton.php");
            $pDatabase = Database::getInstance();
            $c = $pDatabase->query("select c.city_title as 'City' from teamshuffle.tbl_city c, teamshuffle.tbl_sport_city s where c.tblcity_id = s.tblcity_id and s.tblsport_id = " . );
            while($r = mysql_fetch_array($c)){
                echo("alert(\"" . $r[0] . $_COOKIE['selr'] . "\");");
                 }
        ?>
    }
}
function setCookie(c_name,value,exdays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

Upvotes: 0

Kaii
Kaii

Reputation: 20540

accessing JS variables in PHP is not possible. However, you could generate javascript with PHP which would look like this and does what you expect it to do:

            while($r = mysql_fetch_array($c)){
                echo 'alert("' . $r[0] . '" + selr);';
            }

note the + selr is part of the echo, so it is sent to the browser and javascript does the string contatenation on the client side.

look at the resulting HTML source in your browser if you don't understand what i mean.

Upvotes: 1

Chad
Chad

Reputation: 19609

Short Answer: You can't. PHP is server side and JavaScript is client side.

Long Answer:

You may not be able to "access" the variable, but you can send the value off in an ajax request to whatever PHP page needs it and use it there. Either as a POST parameter or Query parameter.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190907

You will have to make an ajax request back to the server and send an appropriate response.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230296

No, you can't do this. PHP is executed on the server, Javascript - in the client's browser. They must communicate through HTTP requests.

Upvotes: 0

Related Questions