2012-07-29 70 views
0

我正在構建我正在構建的PHP身份驗證系統的前端,我使用jQuery,我知道或多或少的基本知識,並且通過登錄,註冊和激活成功使用它頁面顯示錯誤/成功消息,在發送之前對密碼進行散列,爲用戶分配cookie並進行表單驗證。jQuery驗證不被稱爲

我目前正在進行重置系統,這部分是第2步,在用戶收到重置請求電子郵件後,他們來到網站並輸入重置代碼,或者使用直接鏈接並點擊按鈕。 jQuery然後發送一個請求到服務器驗證重置鍵,如果不正確,顯示正確的消息,如果正確,清除.form div並用另一個表單(#resetpass)替換它。在這種形式下,用戶鍵入他們的新密碼,然後將其發送到服務器,但由於某種原因,jQuery沒有捕獲提交,並且表單正常提交,而不是通過Ajax,我一直在查看代碼相當長一段時間,改變這個和那個,沒有積極的結果。我究竟做錯了什麼 ?

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>PHPAuth 2.0 - Reset Password</title> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
<!-- Base CSS --> 
<link href="css/reset.css" rel="stylesheet" type="text/css" /> 
<link href="css/style.css" rel="stylesheet" type="text/css" /> 
<!-- Jquery CSS (UI + Addons) --> 
<link href="css/ui-lightness/jquery-ui.custom.css" rel="stylesheet" type="text/css" /> 
<link href="css/jquery.qtip.css" rel="stylesheet" type="text/css" /> 
<!-- Jquery base file + Jquery UI --> 
<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui.message.js"></script> 
<!-- Jquery plugins for this page : To be compiled --> 
<script type="text/javascript" src="js/reset/jquery.validate.js"></script> 
<script type="text/javascript" src="js/reset/jquery.qtip.js"></script> 
<script type="text/javascript" src="js/reset/jquery.crypt.js"></script> 
</head> 

<body> 
<div class="logo"></div> 
<div class="form"> 
    <form id="reset" method="post"> 
     <input type="text" name="resetkey" id="password" placeholder="Reset Key" <?php if(isset($_GET['key'])) { echo "value=\"" . $_GET['key'] . "\" "; } ?>/> 
     <input type="submit" id="reset" value="Reset Password" /> 
    </form> 
</div> 
<div class="small"> 
    <a href="?page=login">I know my password</a><br/> 
</div> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#reset').validate({ 
      errorClass: "errormessage", 
      onkeyup: false, 
      errorClass: 'error', 
      validClass: 'valid', 
      rules: { 
       password: { required: true, minlength: 3, maxlength: 100 } 
      }, 
      errorPlacement: function(error, element) 
      { 
       var elem = $(element), 
        corners = ['right center', 'left center'], 
        flipIt = elem.parents('span.right').length > 0; 

       if(!error.is(':empty')) { 
        elem.filter(':not(.valid)').qtip({ 
         overwrite: false, 
         content: error, 
         position: { 
          my: corners[ flipIt ? 0 : 1 ], 
          at: corners[ flipIt ? 1 : 0 ], 
          viewport: $(window) 
         }, 
         show: { 
          event: false, 
          ready: true 
         }, 
         hide: false, 
         style: { 
          classes: 'ui-tooltip-red' 
         } 
        }) 
        .qtip('option', 'content.text', error); 
       } 
       else { elem.qtip('destroy'); } 
      }, 
      success: $.noop, 
    }) 


    $('#resetpass').validate({ 
      errorClass: "errormessage", 
      onkeyup: false, 
      errorClass: 'error', 
      validClass: 'valid', 
      rules: { 
       resetkey: { required: true, minlength: 20, maxlength: 20 } 
      }, 
      errorPlacement: function(error, element) 
      { 
       var elem = $(element), 
        corners = ['right center', 'left center'], 
        flipIt = elem.parents('span.right').length > 0; 

       if(!error.is(':empty')) { 
        elem.filter(':not(.valid)').qtip({ 
         overwrite: false, 
         content: error, 
         position: { 
          my: corners[ flipIt ? 0 : 1 ], 
          at: corners[ flipIt ? 1 : 0 ], 
          viewport: $(window) 
         }, 
         show: { 
          event: false, 
          ready: true 
         }, 
         hide: false, 
         style: { 
          classes: 'ui-tooltip-red' 
         } 
        }) 
        .qtip('option', 'content.text', error); 
       } 
       else { elem.qtip('destroy'); } 
      }, 
      success: $.noop, 
    }) 
}); 

$("#reset").submit(function(event) { 
    if($("#reset").valid()) { 
     event.preventDefault(); 

     var $form = $(this), 
      resetkey = $form.find('input[name="resetkey"]').val(); 


     $.post("inc/action.php?a=reset2", {key: resetkey}, 
      function(data) { 
       if(data['error'] == 1) 
       { 
        $("#message").remove(); 

        $(".form").prepend('<div id="message"></div>'); 

        $("#message").message({type: "error", dismiss: false, message: data['message']}); 

        $("#message").effect("shake", {times: 2, distance: 10}, 200); 
       } 
       else if(data['error'] == 0) 
       {       
        $(".form").empty(); 

        $(".form").append('<form id="resetpass" method="post">'); 
        $("#resetpass").append('<input type="hidden" name="resetkey" value="' + resetkey + '" />'); 
        $("#resetpass").append('<input type="password" name="password" id="password" placeholder="New Password" />'); 
        $("#resetpass").append('<input type="submit" id="reset" value="Change Password" />'); 
       } 
      }, "json" 
     ); 
    } 
    else 
    { 
     $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300); 
     return false; 
    } 
}); 

$("#resetpass").submit(function(event) { 
    if($("#resetpass").valid()) { 
     event.preventDefault(); 

     var $form = $(this), 
      resetkey = $form.find('input[name="resetkey"]').val(), 
      pass = $().crypt({method:"sha1",source:$().crypt({method:"sha1",source:$form.find('input[name="password"]').val()})}); 

     $.post("inc/action.php?a=reset3", {key: resetkey, password: pass}, 
      function(data) { 
       if(data['error'] == 1) 
       { 
        $("#message").remove(); 

        $(".form").prepend('<div id="message"></div>'); 

        $("#message").message({type: "error", dismiss: false, message: data['message']}); 

        $("#message").effect("shake", {times: 2, distance: 10}, 200); 
       } 
       else if(data['error'] == 0) 
       {   
        $("#message").remove(); 

        $(".form").prepend('<div id="message"></div>'); 

        $("#message").message({type: "info", dismiss: false, message: data['message']}); 

        $("#message").effect("pulsate", {times: 2}, 200); 
       } 
      }, "json" 
     ); 
    } 
    else 
    { 
     $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300); 
     return false; 
    } 
}); 
</script> 
</body> 
</html> 

任何幫助是極大的讚賞:)

回答

0

問題解決由於Shoky在irc.freenode.net上的#jquery

只是不得不拋出所有我的驗證代碼並在AFTER中提交我已經將這些東西添加到DOM中, window_timeout函數。

0

爲什麼你有2個變量與相同的ID '復位' 嘗試,而不是reset.submit

<form id="resetfrm" method="post" onsubmit="fnSubmit();return false;"> 

function fnSubmit(){ 
    if($("#reset").valid()) {  
     var $form = $(this), 
      resetkey = $form.find('input[name="resetkey"]').val(); 


     $.post("inc/action.php?a=reset2", {key: resetkey}, 
      function(data) { 
       if(data['error'] == 1) 
       { 
        $("#message").remove(); 

        $(".form").prepend('<div id="message"></div>'); 

        $("#message").message({type: "error", dismiss: false, message: data['message']}); 

        $("#message").effect("shake", {times: 2, distance: 10}, 200); 
       } 
       else if(data['error'] == 0) 
       {       
        $(".form").empty(); 

        $(".form").append('<form id="resetpass" method="post">'); 
        $("#resetpass").append('<input type="hidden" name="resetkey" value="' + resetkey + '" />'); 
        $("#resetpass").append('<input type="password" name="password" id="password" placeholder="New Password" />'); 
        $("#resetpass").append('<input type="submit" id="reset" value="Change Password" />'); 
       } 
      }, "json" 
     ); 
    } 
    else 
    { 
     $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300); 
     return false; 
    } 

} 
+0

現在沒有任何反應,當我嘗試提交表格 – 2012-07-29 17:25:06

+0

你改變了ID嗎?已編輯答案 – Dru 2012-07-29 17:26:33

+0

是的,但我不會在jQuery驗證腳本中的其他地方更改ID,例如$(「#resetpass」)。validate()? – 2012-07-29 17:32:01