Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
        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 . "\");");
share|improve this question
1  
Uhm...server-side VS client-side. You can't unless using ajax; when the JS executes, php has long ended its work –  Damien Pirsy Dec 21 '11 at 13:58
add comment

5 Answers

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.

share|improve this answer
    
Some angry soul downvoted everyone in this topic :-) –  Sergio Tulentsev Dec 21 '11 at 14:11
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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;
}
share|improve this answer
add comment

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.