简体   繁体   English

JavaScript传递变量并从PHP页面检索输出

[英]JavaScript to Pass variable and retrieve output from PHP page

Trying to pass a variable to a PHP file then get the output of that file. 尝试将变量传递给PHP文件,然后获取该文件的输出。

Page1.html: 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: textandemail.php:

  $variable = $_POST["txt"];
  $variable2 = $_POST["email"];
  echo $variable;
  echo $variable2;    

Hopefully this describes the desired idea. 希望这可以描述所需的想法。 I will be doing much more in the PHP file but in the end echoing out a response that I want the JavaScript to read and implement into the html page. 我将在PHP文件中做更多的工作,但最后会回显我希望JavaScript读取并在html页面中实现的响应。

Your post call is asynchronous, you have access to the data2 variable only once the process is done, so you should do your ....innerHTML ... in the callback function, when the data is available, not before. 您的帖子调用是异步的,只有在处理完成后才可以访问data2变量,因此,当数据可用时,您应该在回调函数中执行....innerHTML ... ,而不能在此之前。

There are plenty of good examples of this on any js sites. 在任何js网站上都有很多很好的例子。 On the jQuery doc you have a good example. jQuery文档上,您有一个很好的例子。 Since you are useing jQuery, you could replace your innerHTML call too. 由于您使用的是jQuery,因此也可以替换您的innerHTML调用。

The $.post() function is asynchronous. $.post()函数是异步的。 It finishes some time LATER. 稍后会结束。 You need to put the assignment of the results from that function into the success handler, not after the function itself because as you have it now, the line document.getElementById("test").innerHTML = data2; 您需要将来自该函数的结果分配放入成功处理程序中,而不是放在函数本身之后,因为如您现在所拥有的那样,行document.getElementById("test").innerHTML = data2; is occurring BEFORE the ajax function has finished and thus it won't work. 在ajax函数完成之前发生,因此它将无法正常工作。

This is how you can do it: 这是您可以执行的操作:

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

Or, since you have jQuery, you can do it like this: 或者,由于您拥有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>

you should use ajax function to make communication between php and javascript 您应该使用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)    

the call functions put it inside the javascript page 调用函数将其放在javascript页面中

Ajax_Send("POST","users.php",data,e)

where data is the data you send to the php page and e is the function you pass the output of php page to it 其中data是您发送到php页面的数据,e是您将php页面的输出传递给它的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM