Passing PHP variables back to JavaScript variables on original page as Ajax

旧城冷巷雨未停 提交于 2019-12-11 12:28:59

问题


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 . "&#8451</td>";
    echo "<td>" . $FFlrPCRoomTemp . "&#8451</td>";
    echo "<td>" . $FFlrStudyRm6Temp . "&#8451</td>";
    echo "<td>" . $FFlrStudyRm8Temp . "&#8451</td>";    

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7083998/passing-php-variables-back-to-javascript-variables-on-original-page-as-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!