使用AJAX运行PHP代码 [英] Using AJAX to run PHP code

查看:87
本文介绍了使用AJAX运行PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,这是我第一次使用PHP,但是我设法很容易地编写了PHP代码.我要尝试做的下一件事是编写一些JavaScript来运行PHP文件,并将XML对象发送到PHP,PHP可以在其中添加和保存XML.我以为我会使用jQuery的$.ajax函数.我开始使用一个简单的示例尝试将字符串回显到某些<pre>标记中.所以这是我的代码:

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 $.ajax function. I started using a simple example trying to echo a string into some <pre> tags. So here is my code:

Javascript(位于index.php中)

Javascript (Lies within index.php)

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

PHP& HTML(也位于index.php中)

PHP & HTML (also lies within index.php)

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

推荐答案

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

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

      始终包括$.ajax(method: 'get')

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

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

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

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

      但最重要的是:

      • 您实际上并没有对返回的数据做任何事情!您还需要$.ajax选项中的success函数,该函数对服务器中的数据进行某些处理.
      • You're not actually doing anything with the returned data! You also need a success function in the $.ajax options that does something with the data from the server.

      尝试以下代码:

      (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和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 } ?>
      

      请注意,PHP代码现在将根据是否正在处理AJAX请求而做不同的事情.对于AJAX请求,您不希望返回该页面的HTML-只是返回您感兴趣的数据.因此,最好有两个页面:一个index.php页面和一个ajax.php页面处理AJAX请求的页面.

      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.php page, and one ajax.php page which handles AJAX requests.

      此外,在这个简单的例子中,您可能只使用了纯HTML表单,不需要任何JavaScript或AJAX.

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

      这篇关于使用AJAX运行PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆