简体   繁体   English

PHP不显示来自AJAX的变量

[英]php not showing variable from AJAX

I am trying to post a variable from javascript to the same php page. 我正在尝试将变量从javascript发布到同一php页面。 Here is the code: 这是代码:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
    </head>
    <body>

    <?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?>
    <?php $thisPage = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>

    <script>
      $(document).ready(function() {
        $('#testing123').on('change',function () {
            var testing = "confirmed";
            $.ajax({  
                type: "GET",  
                url: "<?php echo $thisPage; ?>",  
                data: testing,
                success: function() { $('#showresult').html("success"); } 
            })
        });
      });
    </script>

    <form>
      <select id="testing123">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
    </form>

    <div id="showresult"></div>

    </body>
</html>

I have also tried using POST but doesn't pick up in php. 我也尝试过使用POST,但没有在php中接听。 Also tried having a separate php file and using include_once() and still doesn't work. 还尝试过使用单独的php文件并使用include_once()仍然无法正常工作。

Am I missing a file to load in? 我是否缺少要加载的文件?

The problem is that you are sending a string that is not a key-value pair. 问题在于您要发送的字符串不是键值对。 You can send a string (that is what happens behind the screens anyway) but it needs to be a valid query string consisting of key - value pairs that are correctly encoded. 您可以发送一个字符串(无论如何在屏幕后面都会发生),但是它必须是一个有效的查询字符串,其中包含正确编码的键-值对。

It is easier to send an object and let jQuery take care of the encoding: 发送对象更容易,让jQuery处理编码:

    $('#testing123').on('change',function () {
        var testing = "confirmed";
        $.ajax({  
            type: "GET",  
            url: "<?php echo $thisPage; ?>",  
            data: testing,
            success: function() { $('#showresult').html("success"); } 
        })
    });

Would become something like: 会变成这样:

    $('#testing123').on('change',function () {
        // send key - value pairs
        var testing = {'testing': "confirmed"};
        $.ajax({  
            type: "GET",  
            url: "<?php echo $thisPage; ?>",  
            data: testing,
            success: function() { $('#showresult').html("success"); } 
        })
    });

Edit: To get the results of your php script back on your page, you need to use the variable that is passed to the success function: 编辑:要使您的php脚本的结果返回页面,您需要使用传递给success函数的变量:

...
success: function(output_from_php_script) {
            $('#showresult').text(output_from_php_script); 
         }

Also note that posting to the original page that generated the page in an ajax call, is not very convenient: It will return you loads of html (the whole page...) that you don't need. 还要注意,在ajax调用中发布到生成该页面的原始页面并不是很方便:它将返回您不需要的html负载(整个页面...)。

You'd better write a separate script to process your ajax calls and have that return ( echo out...) only what you need. 您最好编写一个单独的脚本来处理您的ajax调用,并仅根据需要返回( echo ...)。

  • Even when you happen to succeed with it, you won't really know whether the output was a success since the ajax request will be successful, but the outcome of your PHP might not be. 即使您碰巧成功,由于ajax请求将成功,您也不会真正知道输出是否成功,但是PHP的结果可能不会成功。 This is probably not what you wanted. 这可能不是您想要的。
  • You don't need to use absolute URL, simply use url: "page.php" 您不需要使用绝对URL,只需使用url: "page.php"

What I would suggest you to do is to create a separate php page that only contains <?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?> 我建议您做的是创建一个单独的php页面,该页面仅包含<?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?> <?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?> <?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?> and the call this page using ajax. <?php echo 'Result is '; if (isset($_GET['testing'])) { echo $_GET['testing']; } ?>然后使用ajax调用此页面。

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

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