原来的页面作为阿贾克斯传递PHP变量返回的JavaScript变量变量、页面、PHP、阿贾克斯

2023-09-10 21:25:49 作者:琪花瑶草怀中抱

我有一些Ajax它通过一个javascript变量getfirstvals.php,然后做了的事情,与DB和回声出根据JavaScript变量I输入值的表。我现在想回传正被呼应,回的JavaScript正在从发送,以便将它们转换为JavaScript变量进行计算在原来的页面,这些PHP变量。我不知道如何做到这一点。

下面是它如何工作至今。

一个范围滑块来选择值:

 回声<输入ID ='滑'类型='范围'
分= \$ vartime [0] \最大= \$ timemax \价值= \$ vartime [0] \步='任何'/>
<跨度ID ='范围'> &所述; /跨度>中
 

......

  selectslider.onchange =功能changecolour(){
    showValue(selectslider.value)
 
欧冠 半场2乌龙 连追3球 切尔西4 4九人阿贾克斯

......

 <脚本类型=文/ JavaScript的>
功能showValue(STR){
如果(STR ==){
    。的document.getElementById(数据表)的innerHTML =;
    返回;
}
如果(window.XMLHtt prequest){// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
    XMLHTTP =新XMLHtt prequest();
}其他{// code对IE6,IE5
    XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能(){
    如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
        的document.getElementById(数据表)的innerHTML = xmlhttp.responseText。
    }
}
如果(floorchoice = 0&安培;!&安培;!floorchoice = 1){
    如果(floorchoice == 2)
    {
    xmlhttp.open(GET,getfirstvals.php Q =?+ STR,真正的);
    xmlhttp.send();
 

......

这将Q,以getfirstvals.php其认定值个DB匹配q和呼应了所有值的同一行入。以这种方式,与q改变回荡值的变化。正如我上面提到的,我想这些价值不仅呼应,但被回传给然后可以用于计算在原来的页面上的JavaScript变量。下面是我的意思的价值观正在附和道:

 回声< TD>中。 $ FFlrOpenPlanTemp。与&#8451&所述; / TD>中;
    回声< TD>中。 $ FFlrPCRoomTemp。与&#8451&所述; / TD>中;
    回声< TD>中。 $ FFlrStudyRm6Temp。与&#8451&所述; / TD>中;
    回声< TD>中。 $ FFlrStudyRm8Temp。与&#8451&所述; / TD>中;
 

解决方案

看看 JSON 。 PHP有一个功能: json_en code这将一个PHP数据对象(即数组,但并不一定是一个数组)和连接code它的JavaScript对象的概念。

在你的JS,你可以再评估。(提示:不要使用评估,现代浏览器都JSON解析器)来获取JSON数据转换为JavaScript对象

示例

如果我在PHP中的数组如下:

$阵列=阵列(测试1=> 1,测试2=> 2,3,阵列(4,5,6));

和我 json_en code 该数组,我风与下面的字符串的:

{测试1:1,测试2:2,0:3,1:[4,5,6]}

这是返回什么回JS(又名,回声出)。您可以通过本地解析功能解析这个 JSON.parse 在Javascript:

VAR OBJ = JSON.parse(pH值preturnStr);

瞧的。你有JS对象从PHP来传递。

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>";    

解决方案

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.