2015-09-06 109 views
-3

我試圖簡單地使用JQuery POST一些東西。當單擊元素時,我希望頁面重定向到php文件並回顯POST值。JQuery非常簡單的POST不工作

HTML文件

<html> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<div onclick="posttest();" >hi there</div> 
<script> 

function posttest(){ 
var txt="hi"; 
$.post("testpost.php", {testVar: txt},function(){ 
    window.location.href = "testpostphp.php"; 
}); 
} 
</script> 
</html> 

PHP文件

<?php 
var_dump($POST["testVar"]); 
?> 
+0

你得到任何錯誤? –

+5

你不明白ajax和頁面重新加載之間的區別。 –

+0

你如何在服務器上存儲這個變量?如果你不是如何預期這個工作? – charlietfl

回答

0
  1. PHP中$_POST變量,而不是$POST
  2. 試試這個:

    $.post("testpost.php", {testVar: txt}, function(response){ 
        console.log(response); 
    });