I am trying to learn the Jquery AJAX function but am struggling to work out how to pass PHP variables into my main document.
我正在尝试学习Jquery AJAX函数,但我正在努力研究如何将PHP变量传递到我的主文档中。
This is what I currently have:
这就是我目前所拥有的:
<script>
var refreshId = setInterval(function() {
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
$('#testdiv').html(data.test1);
$('#testdiv').append(html(data.test2));
//parse the json data
}
});
}, 1000);
</script>
5 个解决方案
#1
5
You should use json
or xml
format and parse it, and get the variable.
您应该使用json或xml格式并解析它,并获取变量。
<script src="jquery.js"></script>
<script>
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
alert(data.test1);
//parse the json data
}
});
</script>
on test.php
在test.php上
<?php
$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';
echo json_encode($test);
//echo nothing after this //not even html
#2
1
use dataType='json' in ajax option on index.php
在index.php的ajax选项中使用dataType ='json'
and on test.php use json_encode
并在test.php上使用json_encode
$ret_array= array ($test1, $test2 and $test3);
echo json_encode($ret_array);
again on index.php
再次在index.php上
now if u want to use it in your *J*S file then access via object navigation or using getJSON
method
现在,如果您想在* J * S文件中使用它,则可以通过对象导航或使用getJSON方法进行访问
and if u want to use that data directly in php then use json_decode
如果你想直接在php中使用那些数据,那么使用json_decode
json_decode($ret_array,true);
References
json_encode getJSON json_decode
#3
1
Another approach (with ajax but hidden field) would be:
另一种方法(使用ajax但隐藏字段)将是:
$.ajax({
type: 'POST',
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
$('#testdiv').html('<input type="hidden" value="' + data.test1 + '" />');
}
error: ajaxError,
dataType: "html"
});
With that inside of your form you can even use your value in the next Postback without passing it directly.
使用表单内部,您甚至可以在下一个Postback中使用您的值而不直接传递它。
#4
-1
You don't pass them into index.php, but you can add them as content that is loaded by your ajax something like this:
你没有将它们传递给index.php,但你可以将它们添加为你的ajax加载的内容,如下所示:
$test1 = '1';
$test2 = '2';
$test3 = '3';
echo $test1 . "<br/>" . $test2 . "<br/>" . $test3 . "<br/>";
#5
-1
Unless im greatly mistaken its as simple as:
除非我非常误以为它简单如下:
<?php
include 'test.php';
?>
To include the test.php file in index.php
在index.php中包含test.php文件
Then you can call them as if they were variables set in index.php, i would then do the following to get them into jQuery:
然后你可以调用它们就像它们是index.php中设置的变量一样,然后我会执行以下操作将它们转换为jQuery:
$("#test1") etc...
$(“#test1”)等......
Update
更新
The problem I have is that the variables in test.php will be changing constantly, and then these need to be updated on index.php. I only set them as 1, 2 and 3 to test it out. – user683526 1 min ago
我遇到的问题是test.php中的变量会不断变化,然后需要在index.php上更新这些变量。我只将它们设置为1,2和3来测试它。 - user683526 1分钟前
In that case set them in a function and return the values.
在这种情况下,将它们设置在一个函数中并返回值。
#1
5
You should use json
or xml
format and parse it, and get the variable.
您应该使用json或xml格式并解析它,并获取变量。
<script src="jquery.js"></script>
<script>
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
alert(data.test1);
//parse the json data
}
});
</script>
on test.php
在test.php上
<?php
$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';
echo json_encode($test);
//echo nothing after this //not even html
#2
1
use dataType='json' in ajax option on index.php
在index.php的ajax选项中使用dataType ='json'
and on test.php use json_encode
并在test.php上使用json_encode
$ret_array= array ($test1, $test2 and $test3);
echo json_encode($ret_array);
again on index.php
再次在index.php上
now if u want to use it in your *J*S file then access via object navigation or using getJSON
method
现在,如果您想在* J * S文件中使用它,则可以通过对象导航或使用getJSON方法进行访问
and if u want to use that data directly in php then use json_decode
如果你想直接在php中使用那些数据,那么使用json_decode
json_decode($ret_array,true);
References
json_encode getJSON json_decode
#3
1
Another approach (with ajax but hidden field) would be:
另一种方法(使用ajax但隐藏字段)将是:
$.ajax({
type: 'POST',
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
$('#testdiv').html('<input type="hidden" value="' + data.test1 + '" />');
}
error: ajaxError,
dataType: "html"
});
With that inside of your form you can even use your value in the next Postback without passing it directly.
使用表单内部,您甚至可以在下一个Postback中使用您的值而不直接传递它。
#4
-1
You don't pass them into index.php, but you can add them as content that is loaded by your ajax something like this:
你没有将它们传递给index.php,但你可以将它们添加为你的ajax加载的内容,如下所示:
$test1 = '1';
$test2 = '2';
$test3 = '3';
echo $test1 . "<br/>" . $test2 . "<br/>" . $test3 . "<br/>";
#5
-1
Unless im greatly mistaken its as simple as:
除非我非常误以为它简单如下:
<?php
include 'test.php';
?>
To include the test.php file in index.php
在index.php中包含test.php文件
Then you can call them as if they were variables set in index.php, i would then do the following to get them into jQuery:
然后你可以调用它们就像它们是index.php中设置的变量一样,然后我会执行以下操作将它们转换为jQuery:
$("#test1") etc...
$(“#test1”)等......
Update
更新
The problem I have is that the variables in test.php will be changing constantly, and then these need to be updated on index.php. I only set them as 1, 2 and 3 to test it out. – user683526 1 min ago
我遇到的问题是test.php中的变量会不断变化,然后需要在index.php上更新这些变量。我只将它们设置为1,2和3来测试它。 - user683526 1分钟前
In that case set them in a function and return the values.
在这种情况下,将它们设置在一个函数中并返回值。