Javascript 使用 AJAX 运行 PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12075143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:03:52  来源:igfitidea点击:

Using AJAX to run PHP code

phpjavascriptajaxjquery

提问by Nolski

I have written some JavaScript code that will read from the Google Maps API and get a list of JSON objects. It will then convert each JSON object into an XML object. A co-worker needs this list converted to XML and then appended to an existing XML file and then save it to our server. So I wrote some PHP code to do that.

我编写了一些 JavaScript 代码,这些代码将从 Google Maps API 读取并获取 JSON 对象列表。然后它将每个 JSON 对象转换为一个 XML 对象。一位同事需要将此列表转换为 XML,然后附加到现有的 XML 文件,然后将其保存到我们的服务器。所以我写了一些 PHP 代码来做到这一点。

Now this is my first time working with PHP but I managed to write that PHP code easily enough. The next thing I am trying to do is write some javascript to run the PHP file and also send the XML object over to the PHP where it can append and save the XML. I figured I would use jQuery's $.ajaxfunction. I started using a simple example trying to echo a string into some <pre>tags. So here is my code:

现在这是我第一次使用 PHP,但我设法轻松地编写了该 PHP 代码。我要做的下一件事是编写一些 javascript 来运行 PHP 文件,并将 XML 对象发送到 PHP,在那里它可以附加和保存 XML。我想我会使用jQuery的$.ajax功能。我开始使用一个简单的例子来尝试将一个字符串回显到一些<pre>标签中。所以这是我的代码:

Javascript (Lies within index.php)

Javascript(位于 index.php 中)

<script>
    var scriptString = 'THIS IS MY STRING';
    $('#clickMe').click(function(){
        $.ajax(
        {
            method:'get',
        url:'index.php',
        data:
        {
            'myString': scriptString
        }
        });
    });
    </script>

PHP & HTML (also lies within index.php)

PHP & HTML(也在 index.php 中)

<button type="button" id="clickMe">CLICK ME TO RUN PHP</button>
<pre>
    <?php
        echo $_GET['myString'];
    ?>
</pre>

回答by We Are All Monica

  • Don't include =in your data variable names (just say 'myString': scriptString)

  • Always include the form submission method for $.ajax( method: 'get')

  • In the PHP code, instead of $myString, use $_GET['myString']

  • Consider using a <button>instead of a <div>to send the data to the server - this is more standard.

  • 不要=在您的数据变量名称中包含(只是说'myString': scriptString

  • 始终包含$.ajax( method: 'get')的表单提交方法

  • 在 PHP 代码中$myString,使用$_GET['myString']

  • 考虑使用 a<button>而不是 a<div>将数据发送到服务器 - 这是更标准的。

But most importantly:

但最重要的是:

  • You're not actually doing anything with the returned data! You also need a successfunction in the $.ajaxoptions that does something with the data from the server.
  • 你实际上并没有对返回的数据做任何事情!您还需要选项中的一个success函数来$.ajax处理来自服务器的数据。

Try this code:

试试这个代码:

(JavaScript)

(JavaScript)

var scriptString = 'THIS IS MY STRING';
$('#clickMe').click(function(){
    $.ajax({
      method: 'get',
      url: 'index.php',
      data: {
        'myString': scriptString,
        'ajax': true
      },
      success: function(data) {
        $('#data').text(data);
      }
    });
});

(PHP and HTML)

(PHP 和 HTML)

<?php
if ($_GET['ajax']) {
  echo $_GET['myString'];
} else {
?>
<button type="button" id="clickMe">CLICK ME TO RUN PHP</button>
<pre id="data"></pre>
<?php } ?>

Note that the PHP code will now do different things based on whether or not it is handling an AJAX request. For AJAX requests, you don't want to return the HTML for the page - just the data you're interested in. For this reason, it might be better to have two pages: one index.phppage, and one ajax.phppage which handles AJAX requests.

请注意,PHP 代码现在将根据它是否正在处理 AJAX 请求来执行不同的操作。对于 AJAX 请求,您不想返回页面的 HTML - 只是您感兴趣的数据。因此,最好有两个页面:一个index.php页面和一个ajax.php处理 AJAX 请求的页面。

Also, in this simple examle, you could have just used a plain HTML form, which wouldn't require any JavaScript or AJAX.

此外,在这个简单的示例中,您可以只使用一个不需要任何 JavaScript 或 AJAX 的普通 HTML 表单。