Reputation: 149
I have some Ajax which passes a javascript variable to getfirstvals.php which then does its thing with the DB and echos out a table of values depending on the javascript variable I input. I now want to pass back these PHP variables which are being echoed, back to the original page that the javascript is being sent from in order to convert them into javascript variables to do calculations on. I'm not sure how to do this.
Below is how it works so far.
A range "slider" to select the values:
echo "<input id='slider' type='range'
min=\"$vartime[0]\" max=\"$timemax\" value=\"$vartime[0]\" step='any' />
<span id='range'> </span>"
......
selectslider.onchange=function changecolour(){
showValue(selectslider.value)
.....
<script type="text/javascript">
function showValue(str) {
if (str == "") {
document.getElementById("datatable").innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("datatable").innerHTML = xmlhttp.responseText;
}
}
if (floorchoice!=0 && floorchoice!=1){
if (floorchoice==2)
{
xmlhttp.open("GET", "getfirstvals.php?q=" + str, true);
xmlhttp.send();
......
This sends "q" to getfirstvals.php which finds values matching q in th DB and echoes out all values in the same row as it. In this way, as q changes the echoed values change. As I mentioned above, I would like these values not only to be echoed, but to be passed back to javascript variables on the original page which can then be used for calculations. Below is what I mean by the values being echoed:
echo "<td>" . $FFlrOpenPlanTemp . "℃</td>";
echo "<td>" . $FFlrPCRoomTemp . "℃</td>";
echo "<td>" . $FFlrStudyRm6Temp . "℃</td>";
echo "<td>" . $FFlrStudyRm8Temp . "℃</td>";
Upvotes: 1
Views: 1485
Reputation: 24661
Take a look at JSON. PHP has a function: json_encode which will take a PHP data object (ie array, but doesn't have to be an array) and encode it in javascript object notion.
In your JS you can then evaluate (tip: do not use eval
, modern browsers have json parsers) to get the JSON data into Javascript object.
Example
If I have the following array in PHP:
$array = array( "test1" => 1, "test2" => 2, 3, array( 4, 5, 6 ));
And I json_encode
that array, I wind up with the following string:
{"test1":1,"test2":2,"0":3,"1":[4,5,6]}
This is what you return back to JS (aka, echo out). You can parse this via the native parsing function JSON.parse
in Javascript:
var obj = JSON.parse(phpReturnStr);
Voila. You have an object in JS passed in from PHP.
Upvotes: 2
Reputation: 15375
First, you could use a Javascript library to make it simpler and cross-browser. Then, I'd suggest you to pass your variables from PHP to JS with json_encode()
. Just take a minute to see how jQuery.getJSON works, and it will be a child's play.
Upvotes: 1