通过jQuery的AJAX传递PHP变量 [英] Passing PHP variables through Jquery AJAX
本文介绍了通过jQuery的AJAX传递PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想学习Jquery的AJAX功能,但我在努力解决如何PHP变量传递到我的主要文件。
这是我目前有:
<脚本>
VAR refreshId =的setInterval(函数(){
$阿贾克斯({
网址:test.php的,
数据类型:JSON,//返回类型的数据是jsonn
成功:功能(数据){//< ---(数据)的JSON格式
$('#testdiv)HTML(data.test1);
$('#testdiv)追加(HTML(data.test2));
//解析JSON数据
}
});
},1000);
< / SCRIPT>
解决方案
您应该使用 JSON
或 XML
格式并解析它,并获得该变量。
<脚本SRC =jquery.js和>< / SCRIPT>
<脚本>
$阿贾克斯({
网址:test.php的,
数据类型:JSON,//返回类型的数据是jsonn
成功:功能(数据){//< ---(数据)的JSON格式
警报(data.test1);
//解析JSON数据
}
});
< / SCRIPT>
在的test.php
< PHP
$测试=阵列();
$测试['的test1'] ='1';
$试验['test2的] ='2';
$测试['TEST3'] ='3';
回声json_en code($测试);
//没有回音后,该//甚至没有HTML
I am trying to learn the Jquery AJAX function but am struggling to work out how to pass PHP variables into my main document.
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>
解决方案
You should use json
or xml
format and parse it, and get the variable.
<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
<?php
$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';
echo json_encode($test);
//echo nothing after this //not even html
这篇关于通过jQuery的AJAX传递PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文