简体   繁体   English

PHP无法捕获Ajax jQuery在同一页面上发送的$ _POST数据

[英]PHP not capture $_POST data send by ajax jquery on same page

I am using ajax to post data on the same page and trying to echo posted data with php with following script. 我正在使用ajax在同一页面上发布数据,并尝试使用以下脚本用php回显发布的数据。

$('button').click(function(){

         $.ajax({               
            type: "post",
            data: $("form").serialize(),
            beforeSend: function(){},
            success: function(data){alert(data)},
            error: function(err) {alert(err.responseText);} 
        })

})

and php script is : 和PHP脚本是:

<?php echo isset($_POST['data']) ?  $_POST['data'] :''; ?>

my html is: 我的html是:

<form>
      <input type="hidden" name="data" value="to_success"/>
      <button type="button">Click Me</button>
</form>

My problem is php does not echo posted data on page, but when i post form data on another php file which is same php script; 我的问题是php不会在页面上回显已发布的数据,但是当我在另一个php文件是相同的php脚本中发布表单数据时; php is able to echo posted data and ajax is alert that. php能够回显已发布的数据,而ajax可以发出警报。 please help me to resolve this issue. 请帮助我解决此问题。 thanks 谢谢

It's difficult to tell without seeing your entire PHP page as one listing, but from your description it sounds like your issue is either because of the way you are declaring your .click() event or the way you are posting to the page. 如果不将整个PHP页面视为一个清单,很难说出来,但是从您的描述看来,您的问题似乎是因为您声明了.click()事件的方式还是由于您将其发布到页面的方式.click() The former is more likely. 前者更有可能。

The $.ajax() request will use an XMLHttpRequest object to POST to your PHP script. $.ajax()请求将使用XMLHttpRequest对象发布到您的PHP脚本。 The PHP script should then take those values and generate the return string from the combination of the plain text in the script and the inserted echo'd values. 然后,PHP脚本应采用这些值,并根据脚本中的纯文本和插入的echo'd值的组合生成返回字符串。 This should then be received by the success method's callback function and alerted to your page as a blob of HTML text in the alert box. 然后, success方法的回调函数将接收到此消息,并在警报框中将您的页面作为HTML文本的一滴来提醒您。

Indeed, this is exactly what happens if I use the following code: 确实,如果使用以下代码,这正是发生的情况:

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script>
    $(document).ready(function() {
        $('button').click(function() {
             $.ajax({               
                type: "post",
                data: $("form").serialize(),
                beforeSend: function() {},
                success: function(data) {
                    alert(data);
                },
                error: function(err) {
                    alert(err.responseText);
                } 
            });
        });
    });
    </script>
</head>
<body>

    <?php echo isset($_POST['data']) ?  $_POST['data'] :''; ?>

    <form>
       <input type="hidden" name="data" value="to_success"/>
       <button type="button">Click Me</button>
    </form>

</body>
</html>

However, if I comment out the lines for $(document).ready(function(){ and its corresponding end }); 但是,如果我注释掉$(document).ready(function(){及其对应的结尾}); , then nothing happens when I click. ,那么当我单击时什么也没有发生。

So, try wrapping your .click() event definition in a $(document).ready() . 因此,请尝试将.click()事件定义包装在$(document).ready()

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

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