在php文件中检索jquery post


Retrieve jquery post in php file

我已经为此搜索了几个小时,似乎仍然找不到解决方案。我正在尝试使用jquery将javascript变量发送到php表单

在一个文件中。。index.php我有以下行:

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

在test.php中,我有

$h = $_POST['name'];
print $h;

但是什么都没有打印出来。如果这是一个重复的问题,很抱歉。我做错了什么?

您没有对服务器返回的数据执行任何操作,您需要访问callback并使用正在打印的数据。

$.post("test.php", { name: "John", time: "2pm" }, function(data){
    //data is what you send back from the server, in this scenario, the $h variable.
    alert(data); 
});

您的代码没有被告知在哪里显示数据。当它收到成功的HTTP响应时,你必须告诉它该怎么办

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

您的代码实际上应该是:

<script type="text/javascript">
$.post("test.php", { name: "John", time: "2pm" }, function(data) {
 // You can change .content to any element you want your response to be printed to.
 $('.content').html(data);
});
</script>