我正在将一条消息('ok')从 PHP 脚本回显到 JQuery ajax 调用。
我正在回显正确的消息,并且当我记录它时它会显示在控制台中,但是没有触发相应的 jquery 函数 - 根据代码我应该得到“您的密码已成功更改”消息,但我只收到“出现问题”消息 - 任何人都可以提出原因吗?
这是PHP的代码:
if(isset($_POST['oldpass'])){
$oldpass = mysql_real_escape_string($_POST['oldpass']);
$newpass = mysql_real_escape_string($_POST['newpass']);
$sql = "SELECT password, salt FROM users WHERE email='$log_email' AND id='$log_id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$current_salt = $row["salt"];
$db_pass = $row["password"];
}
$old_pass_hash = crypt($oldpass, $current_salt);
if ($old_pass_hash != $db_pass){
echo "problem";
exit();
}
}
$s = "$2a$10$";
$random = randStrGen(20);
$salt = $s.$random;
$p_crypt = crypt($newpass, $salt);
$sql = "UPDATE users SET password='$p_crypt', salt='$salt' WHERE email='$log_email' AND id='$log_id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
if ($query == true){
$_SESSION['password'] = $p_crypt;
echo 'ok';
exit();
}
}
?>
这是 javascript/JQuery
function change_password(){
var oldpass = $('#old_pass').val();
var newpass = $('#new_pass').val();
var newpass_conf = $('#confirm_new_pass').val();
if(newpass != newpass_conf){
$('#status').html("Your passwords do not match");
} else if(newpass=="" || oldpass==""){
$('#status').html("You have not entered anything");
}
$.ajax({
type: 'POST',
url: "changePassword.php",
dataType: 'text',
data: {
"oldpass": oldpass,
"newpass": newpass_conf },
success:function(data){
if(data == "ok"){
$('#change_password_form').html("<h2> Success</h2><div class='noerror'><p> Your password has been changed successfully.</p> <p> You may now close this window.</p></div>");
} else {
$('#status').html("There was a problem");
}
}
});
}
$(document).ready(function(){
$(document).on('click','#change_password', function(){
change_password();
});
});
</script>
最后是html
<div> <h1>Change your password</h1></div><hr>
<form id="change_password_form" class="input" onsubmit="return false;">
<div> <label for="old_pass">Current Password:</label>
<input id="old_pass" type="text" class="searchbox" onfocus="emptyElement('status')" maxlength="88" value=""></div>
<div> <label for="new_pass">New Password:</label>
<input id="new_pass" class="searchbox" type="text" onfocus="emptyElement('status')" maxlength="88" value=""> </div>
<div><label for="confirm_new_pass">Confirm New Password:</label>
<input id="confirm_new_pass" class="searchbox" type="text" onfocus="emptyElement('status')" maxlength="88" value=""><div>
<input type="button" style="position:relative;top:10px; float:right;" id="change_password" value="Change Password"></form>
<span id="status" class="statuserror"></span>
</body>
</html>