2013-06-26 55 views
0

我一直在尋找這個小時,但仍無法找到解決方案。我試圖使用jQuery JavaScript變量發送到PHP的形式在php文件中檢索jquery文章

在一個文件..的index.php我有以下行:

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

,我有

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

但沒有打印。對不起,如果這是一個重複的問題。我究竟做錯了什麼?

+1

什麼是你的控制檯說? – tymeJV

+0

@tymeJV什麼都不打印。 – user1011332

+0

您的JavaScript控制檯與php無關@ user1011332 –

回答

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); 
}); 
+0

我的主要目標是將這些js變量轉移到php ...它仍然不是加工。 – user1011332

+0

當你說「它不工作」,你是什麼意思?你怎麼知道它沒有將JS變量傳遞給PHP?你期望發生什麼? – Steve

+0

對不起。我是一個小菜鳥。我的意思是我只是試圖通過PHP和我所做的任何訪問JS變量..似乎沒有工作(包括上面張貼的片段) – user1011332

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>