[英]how to auto refresh multiple php variables without refreshing page? I can only do one atm
[英]How can I capture php session variables in javascript without page refresh?
我有一個index.php
,在那里我做session_start()
等。登錄,我發送一個Ajax請求receiver.php
,在這里我設置會話變量等,並得到一個Ajax響應回來。
現在, 僅當刷新具有以下代碼的index.php
頁面時 ,我才能完美捕獲php會話變量:
jsSessionUserId = <?php
if (isset($_SESSION['userId'])) { //LoggedIn:
echo json_encode($_SESSION['userId']);
} else { // Not logged in.
// some code here
}
?>;
我真正想要的是將其放入函數中,然后在收到成功的ajax響應時調用此函數(因此無需刷新index.php
頁面)。 它不起作用。 我懷疑php不能在javascript函數內部正常工作。 感謝您的幫助。
混在一起是個壞主意。 像php和javascript。 目前最適合您的解決方案-收到成功的登錄響應后,覆蓋您的javascript變量。
$.post("receiver.php", formData, function(response){ // login callback
if(response.UserId){ // return user id if login is successful
jsSessionUserId = response.UserId; // overwrite old variable
}
}, "json");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.