将PHP变量作为Ajax传递回原始页面上的JavaScript变量

时间:2022-12-05 13:59:19

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.

我有一些Ajax,它通过一个javascript变量来获得firstvals。php,然后用DB做它的事情,根据javascript变量I的输入返回一个值表。现在我想把这些正在被回显的PHP变量返回到javascript发送的原始页面,以便将它们转换为javascript变量进行计算。我不知道该怎么做。

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:

这将“q”发送到getfirstvals。php查找匹配q的值,并将所有值回传到同一行中。这样,当q改变回显值时。正如我上面提到的,我希望这些值不仅被回显,而且被返回到原始页面上的javascript变量中,然后用于计算。以下是我所说的被呼应的价值观:

        echo "<td>" . $FFlrOpenPlanTemp . "&#8451</td>";
    echo "<td>" . $FFlrPCRoomTemp . "&#8451</td>";
    echo "<td>" . $FFlrStudyRm6Temp . "&#8451</td>";
    echo "<td>" . $FFlrStudyRm8Temp . "&#8451</td>";    

2 个解决方案

#1


2  

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.

看看JSON。PHP有一个函数:json_encode,它将获取一个PHP数据对象(ie数组,但不一定是数组)并将其编码为javascript对象概念。

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.

在您的JS中,您可以评估(提示:不要使用eval,现代浏览器有json解析器)将json数据获取到Javascript对象中。

Example

例子

If I have the following array in PHP:

如果我有以下PHP数组:

$array = array( "test1" => 1, "test2" => 2, 3, array( 4, 5, 6 ));

$数组=阵列(“test1”= > 1,“test2”= > 2,3,阵列(4、5、6));

And I json_encode that array, I wind up with the following string:

我对这个数组进行json_encode,最后得到如下字符串:

{"test1":1,"test2":2,"0":3,"1":[4,5,6]}

{“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:

这就是返回到JS(即echo out)的内容。您可以通过本机解析函数JSON解析它。在Javascript解析:

var obj = JSON.parse(phpReturnStr);

var obj = JSON.parse(phpReturnStr);

Voila. You have an object in JS passed in from PHP.

瞧。在JS中有一个对象从PHP传入。

#2


1  

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.

首先,您可以使用Javascript库来简化和跨浏览器。然后,我建议您使用json_encode()将变量从PHP传递给JS。花一分钟看看jQuery是如何工作的。getJSON是有效的,它将是一个孩子的游戏。

#1


2  

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.

看看JSON。PHP有一个函数:json_encode,它将获取一个PHP数据对象(ie数组,但不一定是数组)并将其编码为javascript对象概念。

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.

在您的JS中,您可以评估(提示:不要使用eval,现代浏览器有json解析器)将json数据获取到Javascript对象中。

Example

例子

If I have the following array in PHP:

如果我有以下PHP数组:

$array = array( "test1" => 1, "test2" => 2, 3, array( 4, 5, 6 ));

$数组=阵列(“test1”= > 1,“test2”= > 2,3,阵列(4、5、6));

And I json_encode that array, I wind up with the following string:

我对这个数组进行json_encode,最后得到如下字符串:

{"test1":1,"test2":2,"0":3,"1":[4,5,6]}

{“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:

这就是返回到JS(即echo out)的内容。您可以通过本机解析函数JSON解析它。在Javascript解析:

var obj = JSON.parse(phpReturnStr);

var obj = JSON.parse(phpReturnStr);

Voila. You have an object in JS passed in from PHP.

瞧。在JS中有一个对象从PHP传入。

#2


1  

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.

首先,您可以使用Javascript库来简化和跨浏览器。然后,我建议您使用json_encode()将变量从PHP传递给JS。花一分钟看看jQuery是如何工作的。getJSON是有效的,它将是一个孩子的游戏。