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 . "\");");
-
1Uhm...server-side VS client-side. You can't unless using ajax; when the JS executes, php has long ended its workDamien Pirsy– Damien Pirsy2011年12月21日 13:58:49 +00:00Commented Dec 21, 2011 at 13:58
5 Answers 5
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.
Comments
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.
Comments
No, you can't do this. PHP is executed on the server, Javascript - in the client's browser. They must communicate through HTTP requests.
Comments
You will have to make an ajax request back to the server and send an appropriate response.
Comments
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;
}