gpt4 book ai didi

javascript - 在 Ajax php 和 mysql 中读取响应

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:05 25 4
gpt4 key购买 nike

我无法让代码工作并重定向到 2 个不同的页面,具体取决于信息是否正确...

到目前为止,我的登录页面上有这个:

$(function () {
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'newfile.php',
data: $('#form').serialize(),
success: function (response){
alert(response);
if (success.text="Username or Password incorrect")
window.location.href = "index.php";
else if (success.text="login successful") {
window.location.href = "login_success.html";
} else {
}
}
})
})

我正在阅读的信息是(来自另一页):

<?php    
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die(" Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$sql="SELECT myusername, mypassword FROM user WHERE myusername = '" . mysqli_real_escape_string($conn, $myusername) . "' and mypassword = '" . mysqli_real_escape_string($conn, $mypassword) . "';";
$result = $conn->query($sql);
if ($result->num_rows >0) {
echo "login successful";
} else {
echo "Username or Password incorrect";
}
$conn->close();

?>

最佳答案

我希望这对你有用试试这个:

if (response=="usernames or Password incorrect") {
window.location.href = "index.php";
}
else if (response=="login successful")
{
window.location.href = "login_success.html";
}
else { }

在ajax中使用这段代码成功。实际上,您在 PHP 中使用了简单的 ECHO,并在 ajax 中成功使用了 response.text。

更新:

您正在使用 = 符号进行比较,应该使用 == 运算符进行比较。

更新 2:

我建议在 php 中使用 status 作为 true false 而不是长字符串,例如:

if ($result->num_rows >0) {
echo true;
} else {
echo false;
}

比起 ajax 响应:

if(response == true){
// Success url
}
else {
// failure url
}

关于javascript - 在 Ajax php 和 mysql 中读取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569682/

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