Sending a variable from PHP to Javascript -
this question exact duplicate of:
- send variable php javascript 2 answers
i know have asked question before, having trouble understanding answers get. have following code in 2 separate files, 1 of them javascript , other php:
javascript
xmlhttp=new xmlhttprequest(); xmlhttp.onreadystatechange=function() { if (this.readystate==4 && this.status==200) { document.getelementbyid("dummy").innerhtml=this.responsetext; } } xmlhttp.open("get","getgames.php?yearfilter="+yearfilter,true); xmlhttp.send(null);
php
$yearfilter = (int)$_request["yearfilter"]; $dummyvariable = 123245;
i have been using javascript file pass variables php cannot figure out how send variable (such dummyvariable example) php javascript file. know result should end in "this.responsetext" don't know code put in php file in order send there. keep being told "echo" variable javascript whenever try seeing variable being printed screen, not seeing evidence being stored in javascript. missing?
you can output varible value this
$dummyvariable = 123245; echo $dummyvariable;
so in javascript
document.getelementbyid("dummy").innerhtml=this.responsetext;
this line add 123245 #dummy id
if want send multiple variable values javascript response json
like that
$dummyarray = json_encode(array("var1"=>"x1","var2"=>"x2")); echo $dummyarray;
now in javascript use line code
var response = json.parse(this.responsetext);
in case response contain values of var1 , var2 ... here how use
alert(response.var1); // return x1; alert(response.var2); // return x2;
Comments
Post a Comment