1
我爲用戶設置了一個設置頁面,並且我的問題是,在您發送表單一次並說您收到「請填寫所有字段」之類的錯誤,然後您去再次提交它不會迴應更多的錯誤或成功的消息,但它會更新您的密碼。Ajax/PHP在第一個帖子後沒有迴應
JS:
<script type="text/javascript">
$(document).ready(function() {
$("#changePassword").click(function(){
var userIdSettings = <?php echo $_SESSION['id']; ?>;
var currPass = $("#currentPass").val();
var newPass = $("#newPass").val();
var newPassRe = $("#newPassRe").val();
$.post("inc/ajax.php", {userIdSettings: userIdSettings, currPass: currPass, newPass: newPass, newPassRe: newPassRe}, function(data){
$(".message").html(data).delay(2000).fadeOut('slow', function(){
});
});
});
});
</script>
PHP:
if ($_POST['userIdSettings']) {
$userIdSettings = $_POST['userIdSettings'];
$currPass = $_POST['currPass'];
$newPass = md5($_POST['newPass']);
$newPassRe = md5($_POST['newPassRe']);
if (!empty($currPass) && !empty($newPass) && !empty($newPassRe)) {
$data = new db();
$data->dbConnect();
$data->dbSelect();
$currPass = md5($currPass);
$checkPass = mysql_query("SELECT * FROM users WHERE id = '$userIdSettings'") or die("Error: ".mysql_error());
$checkPass = mysql_fetch_assoc($checkPass);
if ($currPass == $checkPass['password']) {
if ($newPass == $newPassRe) {
mysql_query("UPDATE users SET password = '$newPassRe' WHERE id = '$userIdSettings'") or die("Error: ".mysql_error());
echo '<div class="messages green large"><span></span>Your password has been updated!</div>';
exit;
} else {
echo '<div class="messages red large"><span></span>Your new passwords dont match!</div>';
exit;
}
} else {
echo '<div class="messages red large"><span></span>Your current password is not correct!</div>';
exit;
}
} else {
echo '<div class="messages red large"><span></span>Please fill in all fields!</div>';
exit;
}
}
完美!謝謝。我對JS有點新鮮。 – user1982365
:)很高興幫助 –
此外,{userIdSettings:userIdSettings,currPass:currPass,newPass:newPass,newPassRe:newPassRe}可能是這個{userIdSettings,currPass,newPass,newPassRe}它是簡寫。 – admcfajn