在嘗試使用Ajax將數(shù)據(jù)從第1頁發(fā)布到第2頁時,第2頁上的if語句讓我知道$_POST未設(shè)置-但盡管未滿足條件,但它仍運行部分代碼,但不是全部代碼。該問題在XAMPP和我的Web服務(wù)器上仍然存在。第1頁上的按鈕以發(fā)布數(shù)據(jù):<button type="button" class="btn postBtn" onclick="location.href='page2.php'" id="1337">Click me</button>通過第1頁上的Ajax發(fā)送數(shù)據(jù):$(document).ready(function(){ $('.postBtn').click(function(){ var id = $(this).attr("id"); $.ajax({ url:"page2.php", method:"post", data:{ id:id, }, }); }); });PHP if第2頁上的語句:if (isset($_POST['id'])) { echo $_POST['id']; $_SESSION['id'] = $_POST['id'];} else { echo "no id posted";}單擊按鈕時,我希望if語句回顯1337并設(shè)置$_SESSION['id'] = 1337。但是,當(dāng)我實際單擊該按鈕時,回顯就會顯示no id posted,但令人驚訝地print_r($_SESSION)顯示[id] => 1337。關(guān)于我所缺少的任何想法?
1 回答

慕蓋茨4494581
TA貢獻1850條經(jīng)驗 獲得超11個贊
如果要使用ajax發(fā)布值,請刪除按鈕的onclick。如果要查看結(jié)果,請在ajax中回顯“添加成功”字段,并在其中管理結(jié)果。
$('.postBtn').click(function(){
var id = $(this).attr("id");
$.ajax({
url:"page2.php",
method:"post",
data:{id:id,},
success: function(res) {
console.log(res);
}
});
});
- 1 回答
- 0 關(guān)注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消