通过AJAX向JavaScript发送PHP变量

时间:2022-09-26 10:52:37

I'm writing a program that will generate random numbers on both JavaScript and PHP pages and display them on the JavaScript/Html page.

我正在编写一个程序,在JavaScript和PHP页面上生成随机数,并在JavaScript/Html页面上显示它们。

So far I have both pages successfully generating the numbers, but I don't know how to reach out from the JavaScript page to the external PHP page to retrieve the number and store it into a JS variable.

到目前为止,我已经成功地生成了这两个页面的编号,但是我不知道如何从JavaScript页面到外部PHP页面检索编号并将其存储到一个JS变量中。

Here's what I have so far.

这是我目前所拥有的。

JavaScript:

JavaScript:

function roll()
    {
        var rollOne; //= Math.floor((Math.random() * 6) + 1);
        var rollTwo;
        var request = new XMLHttpRequest();

        request.open("GET", "filename.php", true);
        request.send();                 
    }

I know the JS random is commented out, that's not important right now.

我知道JS random被注释掉了,现在这不重要。

PHP:

PHP:

<?php
    sleep(5);
    $random =(rand(1,6));
    echo $random;
?>

So how do I take $random from the php document, send it over to the JavaScript page, and store it into a variable to access later?

那么,如何从php文档中取出$random,将其发送到JavaScript页面,并将其存储到一个变量中以供以后访问呢?

I'm sure a similar question has been asked thousands of times before on this site, but from what I have searched I haven't found anything that made sense to me.

我确信在这个网站上,类似的问题已经被问过几千次了,但是从我搜索的内容来看,我没有发现任何对我有意义的东西。

2 个解决方案

#1


2  

The Mozilla docs on AJAX explain it well. Before calling .open and .send, set up a function for XMLHttpRequest to run when the response comes back from the server:

AJAX上的Mozilla文档很好地解释了这一点。在调用.open和.send之前,设置一个函数,让XMLHttpRequest在服务器返回响应时运行:

request.onreadystatechange = function() {
  if (request.readyState === XMLHttpRequest.DONE) {
    // The request is complete
    if (request.status === 200) {
      // Server responded with HTTP status code 200 (OK)
      // Here's your server's random value
      random = request.responseText;
    } else {
      alert('There was a problem with the request.');
    }
  }
}

#2


0  

You can access the responseText value of the returned data. In other words, the data that you echo from php can be accessed in your javascript using responseText and stored in a variable.

您可以访问返回数据的responseText值。换句话说,可以使用responseText在javascript中访问从php返回的数据,并将其存储在一个变量中。

#1


2  

The Mozilla docs on AJAX explain it well. Before calling .open and .send, set up a function for XMLHttpRequest to run when the response comes back from the server:

AJAX上的Mozilla文档很好地解释了这一点。在调用.open和.send之前,设置一个函数,让XMLHttpRequest在服务器返回响应时运行:

request.onreadystatechange = function() {
  if (request.readyState === XMLHttpRequest.DONE) {
    // The request is complete
    if (request.status === 200) {
      // Server responded with HTTP status code 200 (OK)
      // Here's your server's random value
      random = request.responseText;
    } else {
      alert('There was a problem with the request.');
    }
  }
}

#2


0  

You can access the responseText value of the returned data. In other words, the data that you echo from php can be accessed in your javascript using responseText and stored in a variable.

您可以访问返回数据的responseText值。换句话说,可以使用responseText在javascript中访问从php返回的数据,并将其存储在一个变量中。