jQuery post无法在php中获取$_post


jQuery post cannot get $_POST in php

我有一个带有onkeyup事件的窗体。我尝试将一个变量发送到我的php脚本,并在div中显示结果。

多亏了这个论坛,我的测试功能完成了一半:

jQuery.post(the_ajax_script.ajaxurl,

如果我继续:1) jQuery("#theForm").serialize(),我收到的回复短信是"Hello World"如果我试图传递一个变量:2) { name: "p" },我得到:-1

JavaScript

function submit_me(){
jQuery.post(
    the_ajax_script.ajaxurl, 
    { name: "p" },
function(response_from_the_action_function){
    jQuery("#txtHint").html(response_from_the_action_function);
    }
);
}

PHP

<?php
function the_action_function(){
$name = $_POST['name'];
echo "Hello World, " . $name;
die();
}
?>

形式

<form id="theForm">
 <input type="text" name="user">
 <input name="action" type="hidden" value="the_ajax_hook">
 <input id="submit_button" value = "Click This" type="button" onkeyup="submit_me()">
<form>

我实际上想要onkeyup="submit_me(this.value, 0)"我通过他们的admin-ajax.php文件在WordPress上做这件事。

这里面的问题在哪里?

编辑

显然,我不得不在数据中添加操作

{ action:'the_ajax_hook', name:"p" }

我猜它的WP需求,而不是jQuery,因为我看到了这样的例子:

$.post("test.php", { name: "John", time: "2pm" }

无处不在。

这样的东西应该可以工作:

<html>
    <head>
        <script>
            $(document).ready(function() {
                $("#my_form").submit(function(event) {
                    event.preventDefault() // to prevent natural form submit action
                    $.post(
                        "processing.php",
                        { name: "p" },
                        function(data) {
                             var response = jQuery.parseJSON(data);
                             $("#txtHint").html(response.hello_world);
                        }
                    );
                });
            });
        </script>
    </head>
    <body>
        <form id="my_form" action="/" method="post">
            <input type="text" name="user" />
            <input name="action" type="hidden" value="the_ajax_hook" />
            <input type="button" name="submit" value = "Click This" />
        </form>
        <div id="txtHint"></div>
    </body>
</html>

然后在处理中.php:

<?php
    $name = $_POST['name'];
    $response['hello_world'] = "Hello World, " . $name;
    echo json_encode($response);
?>