gpt4 book ai didi

javascript - Jquery不执行简单的条件

转载 作者:行者123 更新时间:2023-12-03 03:34:19 25 4
gpt4 key购买 nike

服务器上的所有内容都运行得很好,但只是这个简单的条件语句由于某种原因不起作用。

$(document).ready(function() {
$("#logsubmit").click(function() {
var username = $("#username").val()
var password = $("#pwd").val()

$.post("functions/logincheck.php", {
username: username,
password: password
}, function(result) {
if (result == "success") {
window.location = "/";
} else {
alert(result);
}
});
});
});

即使result == successelse语句由于某种原因正在执行。

PHP 脚本:

<?php
session_start();
include '../connect.php';
$username = $_POST['username'];
$password = $_POST['password'];

$lgchk = $con->prepare("select uid, pass from userlogin where uname = :uname");
$lgchk->bindParam(':uname', $username);
$lgchk->execute();

if ($lgchk->rowCount() > 0) {
while ($row = $lgchk->fetch(PDO::FETCH_ASSOC)) {
$pass = htmlentities($row['pass']);
$id = htmlentities($row['uid']);
}
if ($password == $pass) {
$_SESSION["myid"] = $id;
echo "success";
} else {
echo "Incorrect Password";
}
} else {
echo "Account Doesnt Exist";
}
?>

最佳答案

做这样的事情

$(document).ready(function() {
$("#logsubmit").click(function() {
var username = $("#username").val()
var password = $("#pwd").val()

$.post("functions/logincheck.php", {
username: username,
password: password
}, function(result) {
var r = result.trim();
if (r == "success") {
window.location = "/";
} else {
alert(result);
}
});
});
});

使用 trim 方法,然后将其分配给其他变量。

希望这对您有帮助。

关于javascript - Jquery不执行简单的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935548/

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