jQuery post() to php page


jQuery post() to php page

我正在学习php/javascript,所以不要微笑。。。

我尝试从page1.php向page2.php发布3个变量。我不确定出了什么问题。。。这是代码(简化模式):

page1.php

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
</head>
<body>
<script type="text/javascript">
window.onload = post_text;
function post_text() {
test1="111";
test2="222";
test3="333";
$.post("page2.php", { test1:test1 , test2:test2, test3=test3 });
}
</script>
</body>
</html>

page2.php

<?php
$a=$_POST['test1'];
$b=$_POST['test2'];
$c=$_POST['test3'];
echo $a.$b.$c;
?>
$.post("page2.php", { test1:test1 , test2:test2, test3:test3 }); 

由于您正在学习,您可能会尝试通过编写较短的代码块来隔离问题,并首先查看它们是否有效。在这种情况下,您的第一个问题是一个普通的拼写错误(test3=test3,而不是test3:test3),因此您的整个JS无法解析。您应该在firebug控制台(或chrome控制台)中看到相关的错误消息。