簡體   English   中英

在PHP文件中檢索jquery帖子

[英]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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM