2011-03-26 49 views
1

我有下面的代碼,其在一定按下按鈕,應驗證一個電子郵件地址(這個功能已經工作),然後應該嘗試發佈自己的狀態,如果它返回「錯誤」,它應該顯示一個錯誤。我想我在某處混合了PHP和JS。找不到這個jQuery代碼的bug

$('#recoverSub').live('click',function() { 
     $("#recoverPost").validate({ 
      rules: { 
       recoverField: { 
        email: true 
       } 
      }, 
      messages:{ 
       recoverField: { 
        email: "Not a valid email." 
       } 
      } 
     }); 
     if($("#recoverPost").valid()) 
     { 
      $.post('php/recoverPost.php', $('#recoverPost').serialize(), function(){ 
       function(data) { 
        if(data != "Error") 
        { 
         $('#recoverPost').hide(); 
         $('#success').show(); 
        } 
        else 
        { 
         echo "This email is not in our records."; 
        } 
       } 
      }); 
     } 
    }); 
+1

那麼哪部分不工作? – BrokenGlass 2011-03-26 20:26:29

回答

2

一更好的方法來實現你想要做的事情:

$(function() { 
    $('#recoverPost').validate({ 
     rules: { 
      recoverField: { 
       email: true 
      } 
     }, 
     messages: { 
      recoverField: { 
       email: "Not a valid email." 
      } 
     }, 
     submitHandler: function(form) { 
      // the form was valid => post it with AJAX 
      $.post('php/recoverPost.php', $(form).serialize(), function(data) { 
       if(data != 'Error') { 
        $('#recoverPost').hide(); 
        $('#success').show(); 
       } 
       else { 
        alert("This email is not in our records."); 
       } 
      }); 
     } 
    });  
}); 
5
else 
{ 
    echo "This email is not in our records."; 
} 

你可能想用的alert而不是echo

alert("This email is not in our records."); 
+0

+1笑,現在我知道他爲什麼使用PHP的標籤。 – JCOC611 2011-03-26 20:28:34

1
echo "This email is not in our records."; 

這是PHP的,就像你想。使用這個來代替:

alert("this email is not in our records."; 
1

$.post已包含函數兩次,第一次沒有獲得通過data所以第二個已經沒有機會了......

應改爲:

$('#recoverSub').live('click',function() { 
     $("#recoverPost").validate({ 
      rules: { 
       recoverField: { 
        email: true 
       } 
      }, 
      messages:{ 
       recoverField: { 
        email: "Not a valid email." 
       } 
      } 
     }); 
     if($("#recoverPost").valid()) 
     { 
      $.post('php/recoverPost.php', $('#recoverPost').serialize(), function(data){ 
        if(data != "Error") 
        { 
         $('#recoverPost').hide(); 
         $('#success').show(); 
        } 
        else 
        { 
         alert("This email is not in our records."); 
        } 
      }); 
     } 
    }); 
+0

和回聲,而不是警告 – 2011-03-26 20:33:13