weixin_33695450 2012-08-22 14:16 采纳率: 0%
浏览 40

使用AJAX运行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.

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 (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 (also lies within index.php)

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

1条回答 默认 最新

  • python小菜 2012-08-22 14:19
    关注
    • 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.

    But most importantly:

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

    Try this code:

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

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

    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊