2013-01-16 57 views
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; 
    } 
} 

回答

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

通知的.show()

要打印的數據頁面,然後使用fadeOut方法,這在最終結果集display:none。然後,您嘗試輸出更多數據,但顯示仍爲none,導致頁面上不顯示任何內容,即使DOM元素正在更新。如果您添加show()方法,這將確保將display的CSS值設置爲block;顯示DOM元素的新文本​​;然後在2秒後慢慢淡出...。

+0

完美!謝謝。我對JS有點新鮮。 – user1982365

+0

:)很高興幫助 –

+0

此外,{userIdSettings:userIdSettings,currPass:currPass,newPass:newPass,newPassRe:newPassRe}可能是這個{userIdSettings,currPass,newPass,newPassRe}它是簡寫。 – admcfajn

相關問題