简体   繁体   English

无法获取 AJAX 发送的 php POST

[英]Can't get php POST sent by AJAX

I know there's a lot of similar questions here, but I looked up over 20 of them, and no solutions worked for me.我知道这里有很多类似的问题,但我查了 20 多个,没有任何解决方案适合我。

Here's the problem: I'm sendind an ajax post value to my index.php.问题是:我正在向我的 index.php 发送一个 ajax 帖子值。 When I look at Firebug, the value is there, but when I try to echo it on the page, the POST is empty.当我查看 Firebug 时,该值就在那里,但是当我尝试在页面上回显它时,POST 为空。 I'm really stucked on this.我真的很坚持这一点。

Here's my full code:这是我的完整代码:

<?php
    if(isset($_POST['action']))
    {
        echo $_POST['action'];
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta charset="utf-8">

    <!-- JQUERY LIBRARY AND SCRIPTS -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
    <a href="index.php">Test</a>

    <script type="text/javascript">
        $(document).ready(function()
        {
            $('a').on('click', function()
            {                
                $.ajax({
                    url: 'index.php',
                    type: 'POST',
                    data: {action: 'clicked'},
                    success: function(data)
                    {
                        console.log(data);
                        alert('Done!');
                    },
                    error: function(jqXHR, textStatus, errorThrown)
                    {
                        alert(errorThrown);
                    }
                });
                return false;
            });
        });
    </script>

</body>
</html>

This code does not try to print that to the page, but simply prepends the whole HTML document with a "clicked" string, in the ajax response.此代码不会尝试将其打印到页面上,而是在 ajax 响应中简单地在整个 HTML 文档前面加上“clicked”字符串。 If you want to show this in the browser, you need print that data to the page.如果要在浏览器中显示此内容,则需要将该数据打印到页面上。 If you inspect the console in FireBug, you will see that the response for the Ajax call is exactly what I described above.如果您检查 FireBug 中的控制台,您将看到对 Ajax 调用的响应正是我上面描述的。

Now if you want to print that value back to your page, here is my suggestion, you create a separate file, ajax.php :现在,如果您想将该值打印回您的页面,这是我的建议,您可以创建一个单独的文件ajax.php

<?php
if(isset($_POST['action']))
{
    echo $_POST['action'];
}
?>

And fix your index.php to include some element where you are going to print that value to.并修复您的 index.php 以包含一些您要将该值打印到的元素。 Ie add <div id="response-results"></div> just after your即在您之后添加<div id="response-results"></div> element.元素。 Then change your Ajax call to go to ajax.php , not index.php.然后将您的 Ajax 调用更改为ajax.php ,而不是 index.php 。

Now you need to populate that Ajax response to the rendered page, and this can be done simply with jQuery like:现在您需要将 Ajax 响应填充到呈现的页面,这可以简单地使用 jQuery 完成,例如:

$("#response-results").html(data);

Ofcourse, this goes into the success handler of the ajax call.当然,这会进入 ajax 调用的成功处理程序。

As Jay Blanchard said You're using AJAX to send aa variable to a page which is already rendered on your browser.正如Jay Blanchard所说,您正在使用 AJAX 将变量发送到已在浏览器上呈现的页面。 This will never work because the PHP your're getting the variable from has been run server-side and returned via AJAX, not in the page you're currently viewing.这永远不会起作用,因为您从中获取变量的 PHP 已在服务器端运行并通过 AJAX 返回,而不是在您当前查看的页面中。 Try this it will work :试试这个它会起作用:

index.php :索引.php :

<?php
    if(isset($_POST['action']))
    {
        echo $_POST['action'];
    }
?>

main.html :主文件:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta charset="utf-8">

    <!-- JQUERY LIBRARY AND SCRIPTS -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
    <a href="#123">Test</a>
    <div id="result"></div>

    <script type="text/javascript">
        $(document).ready(function()
        {
            $('a').on('click', function()
            {                
                $.ajax({
                    url: 'index.php',
                    type: 'POST',
                    data: {action: 'clicked'},
                    success: function(data)
                    {
                        // console.log(data);
                        $("#result").html(data);
                    },
                    error: function(jqXHR, textStatus, errorThrown)
                    {
                        alert(errorThrown);
                    }
                });
                return false;
            });
        });
    </script>

</body>
</html>

As RJ commented, it's impossible to do what I was trying to.正如RJ评论的那样,我不可能做我想做的事情。

That happens because once my page is loaded, the top PHP script is proccessed, but there's nothing on my POST.发生这种情况是因为一旦我的页面被加载,顶部的 PHP 脚本就会被处理,但我的 POST 上没有任何内容。

After my call to Ajax, the page is rendered again but the top PHP script will get nothing cause it's SERVER SIDE .在我调用 Ajax 之后,页面再次呈现,但顶部的 PHP 脚本不会得到任何东西,因为它是SERVER SIDE Turns out that I even could print out my Ajax data on the page, but the PHP $_POST would never get its value.结果我什至可以在页面上打印出我的 Ajax 数据,但是 PHP $_POST 永远不会得到它的值。

Thank you guys.谢谢你们。

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

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