0

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

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

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

在 test.php 中,我有

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

但没有打印任何内容。对不起,如果这是一个重复的问题。我究竟做错了什么?

4

2 回答 2

5

您没有对服务器返回的数据做任何事情,您需要访问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); 
});
于 2013-06-26T19:16:36.263 回答
0

您的代码没有被告知在哪里显示数据。当它收到成功的 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>
于 2013-06-26T19:21:17.410 回答