将 PHP 变量作为 Ajax 传递回原始页面上的 JavaScript 变量
我有一些 Ajax,它将 javascript 变量传递给 getfirstvals.php,然后它对数据库执行操作,并根据我输入的 javascript 变量回显出值表。我现在想将这些正在回显的 PHP 变量传回发送 javascript 的原始页面,以便将它们转换为 javascript 变量来进行计算。我不知道该怎么做。
以下是迄今为止它的工作原理。
用于选择值的范围“滑块”:
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();
这将“q”发送到 getfirstvals.php ,
它在数据库中查找与 q 匹配的值并回显出中的所有 与它同一行。这样,当 q 改变时,回显值也会改变。正如我上面提到的,我希望这些值不仅能够被回显,而且能够传递回原始页面上的 JavaScript 变量,然后可以用于计算。以下是我所说的回显值的含义:
echo "<td>" . $FFlrOpenPlanTemp . "℃</td>";
echo "<td>" . $FFlrPCRoomTemp . "℃</td>";
echo "<td>" . $FFlrStudyRm6Temp . "℃</td>";
echo "<td>" . $FFlrStudyRm8Temp . "℃</td>";
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>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 JSON。 PHP 有一个函数: json_encode 它将接受 PHP 数据对象(即数组,但不一定是数组)并将其编码为 javascript 对象概念。
然后,您可以在 JS 中进行评估(提示:不要使用
eval
,现代浏览器具有 json 解析器),将 JSON 数据获取到 Javascript 对象中。示例
如果我在 PHP 中有以下数组:
$array = array( "test1" => 1, "test2" => 2, 3, array( 4, 5, 6 ));
我
json_encode
该数组,最终得到以下字符串:{"test1":1,"test2":2,"0":3,"1":[4,5,6]}
这是你返回给 JS 的内容(又名 echo出去)。您可以通过 Javascript 中的本机解析函数
JSON.parse
来解析它:var obj = JSON.parse(phpReturnStr);
瞧。你有一个从 PHP 传入的 JS 对象。
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.
首先,您可以使用 Javascript 库来使其更简单且跨浏览器。然后,我建议您使用 json_encode() 将变量从 PHP 传递到 JS。只需花一点时间看看 jQuery.getJSON 是如何工作的,这将是小菜一碟。
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.