gpt4 book ai didi

javascript - 使用 AJAX 登录,其中数据位于文本文件中

转载 作者:行者123 更新时间:2023-11-30 21:08:02 25 4
gpt4 key购买 nike

我正在使用 ajax 登录,其中数据存储在文本文件中,由于某种原因我的登录不起作用,它显示登录失败以及 php 回显登录:错误。这是我的 javascript 代码位:

//Logs in the user
btnLoginForm.addEventListener( "click", loginUser);

function loginUser() {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
ajUserDataFromServer = JSON.parse( this.responseText );

console.log("The return from the server = " + ajUserDataFromServer);

if( ajUserDataFromServer.login == "ok" ){

console.log("WELCOME");
}

else
{

console.log("LOGIN FAIL - TRY AGAIN");

}
}
}
ajax.open( "POST", "api_login_users_wm.php" , true );
var jFrmLogin = new FormData( frmLogin );
ajax.send( jFrmLogin );
}

这是 php:

session_start();

// Load all the users and decode them to an array
$sajUsers = file_get_contents('webshop_mandatory_users.txt');
$ajUsers = json_decode($sajUsers);//turns the string into an array.

// Data comes from the browser
$sEmail = $_POST['txtEmail'];
$sPassword = $_POST['txtPassword'];


// begin looping through the array.
for( $i = 0; $i < count($ajUsers) ; $i++ ) {

//check if the data from the frontend matches any date in the backend - Check each one, one by one.
if ($sEmail == $ajUsers[$i]->email && $sPassword == $ajUsers[$i]->password) { //checks if the value of the username is equal to the value in the array.
echo $sjResponse = '{"login":"ok"}';
exit; //end the if statement and exit if it works.
} else {
echo $sjResponse = '{"login":"error"}'; // it didnt work.
exit;
}
}


?>

最佳答案

此处您的结果将仅与第一个元素匹配。所以你需要使用标志变量来匹配数据

$match_found = false;
for( $i = 0; $i < count($ajUsers) ; $i++ ) {

//check if the data from the frontend matches any date in the backend - Check each one, one by one.
if ($sEmail == $ajUsers[$i]->email && $sPassword == $ajUsers[$i]->password) { //checks if the value of the username is equal to the value in the array.
$match_found = true;
break;
}
}

if($match_found )
{
echo '{"login":"ok"}';
}
else
{
echo '{"login":"ok"}';
}
exit;

关于javascript - 使用 AJAX 登录,其中数据位于文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com