JavaScript传递变量并从PHP页面检索输出
JavaScript to Pass variable and retrieve output from PHP page
尝试将变量传递到PHP文件,然后获得该文件的输出。
Page1.html:
<h2 id="test"></h2>
<script>
var data2;
$.post('textandemail.php',{ txt: "John", email: "test@gmail.com" },
function(data) {
data2 = data;});
document.getElementById("test").innerHTML = data2;
</script>
textandemail.php:
$variable = $_POST["txt"];
$variable2 = $_POST["email"];
echo $variable;
echo $variable2;
希望这能描述出想要的想法。我将在PHP文件中做更多的工作,但最终会返回一个响应,我希望JavaScript读取该响应并将其实现到html页面中。
$.post()
函数是异步的。它稍后结束。您需要将该函数的结果分配到成功处理程序中,而不是在函数本身之后,因为现在,行document.getElementById("test").innerHTML = data2;
发生在ajax函数完成之前,因此它无法工作。
这就是你可以做到的:
<h2 id="test"></h2>
<script>
$.post('textandemail.php',{ txt: "John", email: "test@gmail.com" },
function(data) {
// any code that uses the ajax results in data must be here
// in this function or called from within this function
document.getElementById("test").innerHTML = data;
}
);
</script>
或者,既然你有jQuery,你可以这样做:
<h2 id="test"></h2>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$.post('textandemail.php',{ txt: "John", email: "test@gmail.com" },
function(data) {
// any code that uses the ajax results in data must be here
// in this function or called from within this function
$("#test").html(data);
}
);
</script>
您的post调用是异步的,只有在进程完成后才能访问data2
变量,因此您应该在数据可用时而不是之前在回调函数中执行....innerHTML ...
。
在任何js网站上都有很多这样的好例子。在jQuery文档中,您有一个很好的例子。由于您正在使用jQuery,您也可以替换innerHTML
调用。
您应该使用ajax函数在php和javascript 之间进行通信
function Ajax_Send(GP,URL,PARAMETERS,RESPONSEFUNCTION){
var xmlhttp
try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
catch(e){
try{xmlhttp=new XMLHttpRequest()}
catch(e){
alert("Your Browser Does Not Support AJAX")}}}
err=""
if (GP==undefined) err="GP "
if (URL==undefined) err +="URL "
if (PARAMETERS==undefined) err+="PARAMETERS"
if (err!=""){alert("Missing Identifier(s)'n'n"+err);return false;}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4){
if (RESPONSEFUNCTION=="") return false;
eval(RESPONSEFUNCTION(xmlhttp.responseText))
}
}
if (GP=="GET"){
URL+="?"+PARAMETERS
xmlhttp.open("GET",URL,true)
xmlhttp.send(null)
}
if (GP="POST"){
PARAMETERS=encodeURI(PARAMETERS)
xmlhttp.open("POST",URL,true)
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.setRequestHeader("Content-length",PARAMETERS.length)
xmlhttp.setRequestHeader("Connection", "close")
xmlhttp.send(PARAMETERS)
调用函数将其放入javascript页面中
Ajax_Send("POST","users.php",data,e)
其中data是您发送到php页面的数据,e是您将php页面的输出传递给它的函数
相关文章:
- 如何通过php页面将数据库值检索到jquery自动完成框中
- PHP无法检索request.send
- Php无法检索动态生成的文本字段
- 无法使用jquery$.ajax访问从php页面检索到的json数据
- 引导预类型不适用于从 php 页面检索的动态数组
- PHP脚本检索JSON文件-如何将其传递给JavaScript
- JavaScript传递变量并从PHP页面检索输出
- PHP代码检索数据从谷歌分析帐户
- 使用jquery将数据从动态php页面检索到javascript
- 如何在PHP中检索JSON对象的值
- 从AJAX调用的PHP中检索JSON返回值
- 我不能从php页面检索JSON数据数组到另一个
- 在PHP中检索JS数据
- 在php中检索json数据:json_decode不工作
- 在cookie中传递JS数组并在PHP中检索
- jquery -通过POST (ajax)发送JSON数据并在php中检索
- 如何在PHP中检索从Javascript传递的数据数组?
- 附加不工作的页面从curl在php中检索
- MsgBox 在 Jquery 中,将信息传递给 PHP 并检索结果
- AJAX POST:无法在PHP中检索已发布的数据数组