1

我已经为用户创建了一个设置页面,我遇到的问题是,在您发送一次表单并说您收到“请填写所有字段”之类的错误之后,您再次提交它不会回显任何更多错误或成功消息,但它会更新您的密码。

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;
    }
}
4

1 回答 1

2
$(".message").html(data).show().delay(2000).fadeOut('slow', function(){});

注意.show()

您正在将数据打印到页面上,然后使用fadeOut最终结果集的方法display:none。然后您尝试输出更多数据,但显示仍然是none,导致页面上没有显示任何内容,即使正在更新 DOM 元素。如果添加show()方法,这将确保 CSS 值display设置为block; 显示 DOM 元素的新文本​​;然后fadeOut...慢慢... 2秒后。

于 2013-01-16T04:36:00.697 回答