2 回答

TA貢獻1876條經驗 獲得超5個贊
*更新下面提到的@Patrick Q 似乎可以回顯數據。您能否嘗試修剪在 javascript/jquery 中收到的數據以檢查意外的空格
data.trim()
如果您想添加更多變量,您可以執行以下解決方案。(或者如果你喜歡的話)
您不應該回顯 ajax 的結果。相反,您應該將其作為 json 返回
PHP文件:
$ajax_result = array():
$ajax_result['success'] = false;
$ajax_result['message'] = 'Incorrect login data';
if(something)
{
$ajax_result['success'] = 'login_success';
$ajax_result['message'] = 'You were logged in. You will be redirected now.';
}
header('Content-type:application/json;charset=utf-8');
echo json_encode($ajax_result);
這會將結果作為數組返回到前端,您可以通過選擇帶有 data.success 或 data.message 等的變量來對其進行操作。
查詢/Javascript:
$.ajax({
url:"sql/login_process.php",
method:"POST",
data:$('#login_form').serialize(),
beforeSend:function(){
$('#login_response').html('<span class="text-info"><i class="fas fa-spinner"></i> Loading response...</span>');
},
success:function(data){
$('form').trigger("reset");
$('#login_response').fadeIn().html(data.message);
setTimeout(function(){
$('#login_response').fadeOut("slow");
}, 7000);
if(data.success == "login_success") location.href = "http://www.example.com/myprofile.php";
}
});
這是最基本的用法。您可以修改它以保護只能從 ajax 查詢等訪問。

TA貢獻1836條經驗 獲得超3個贊
老實說,我喜歡這里的 Patrick Q Idea,這是我到目前為止所取得的成就。
阿賈克斯
success:function(data){
if(data != null && data == "success"){ //redirect...
window.location = "http://google.com";
} else { //report failure...
$('form').trigger("reset");
$('#login_response').fadeIn().html(data);
setTimeout(function(){
$('#login_response').fadeOut("slow");
}, 7000);
}
}
PHP
if($num > 0)
{
//if record found
$_SESSION["loggedin"] = TRUE;
$_SESSION['cid']=$num['cid'];
echo "success";
}
- 2 回答
- 0 關注
- 116 瀏覽
添加回答
舉報