2013-05-21 52 views
0

叫我使用JQuery驗證來自:http://bassistance.de/jquery-plugins/jquery-plugin-validation/JQuery的驗證 - 成功函數在未能有效提交

我已經寫了JQuery的,我需要這驗證正確但是我提交功能的最後一部分不能正常工作。

$("#commentForm").validate({ 
    rules: { 
     email_options_name: { 
      required: true 
     }, 
     email_options_company: { 
      required: true 
     }, 
     email_options_country: { 
      required: true 
     }, 
     email_option_email: { 
      required: true, 
      email: true 
     }, 
     check_options: { 
      required: true, 
      minlength: 1 
     } 
    },  
}); 

$("#commentForm").submit(function(){ 

    var valid = $("#commentForm").valid(); 
    if (valid) { 
       $.post('scripts/process_email_initial_form.php',$(this).serialize(), function(o) { 
        location.reload(); 
       }, 'json'); 
      } else { 
       $('.full-error').show(); 
      } 

       return false; 
}); 

$.extend($.validator.messages, { 
      required: "!", 
      email: "!" 
}); 

}

當提交有效腳本process_email_initial_form.php被調用,並正確處理(從尋找到螢火蟲),如果不股利不顯示。問題是當腳本運行完成時location.reload();沒有調用。理想情況下,我想直接進入成功頁面,但這也不起作用。

我試圖在process_email_initial_form.php的末尾放置一個php頭函數,但這也不起作用。

任何幫助,非常感謝。

+0

[閱讀文檔](http://docs.jquery.com/Plugins/Validation/validate#toptions)。 **使用'submitHandler'回調函數。**這是你放置'ajax'的地方。您的外部'submit'處理程序正在搞砸插件的內置提交處理程序。 – Sparky

回答

3

按照docs,該submitHandler回調是 '正確的地方通過Ajax提交表單驗證它之後。'

$("#commentForm").validate({ 
    rules: { 
     // your rules here 
    }, 
    submitHandler: function(form) { 
     // example below uses the 'form' plugin 
     // http://www.malsup.com/jquery/form/#api 
     $(form).ajaxSubmit({ 
      success: function(){ 
       window.location.href="newPage.php" 
      } 
     }); 
    }  
}); 
+0

+1我們有一個贏家。 – Sparky

0

您只檢查表格是否有效。通過檢查這將只返回布爾值,它不會設置錯誤消息。在顯示之前,您需要將錯誤分配給$('。full-error')。

嘗試

$("#commentForm").validate(); 
0

你有沒有嘗試使用done事件?

$.post(
    'scripts/process_email_initial_form.php', 
    $(this).serialize(), 
    function(o) { 
     console.log('success'); 
    }, 
    'json' 
).done(function(o){ 
    window.location.replace("success.php"); 
}); 

您的ajax頁面中的PHP標題不會將當前頁面重定向到成功頁面。它只會讓你的ajax頁面重定向到定義的頭部。

而不是使用window.location.replace你可以嘗試使用jQuery提交表單。

$("#commentForm").submit(function(){ 
    var valid = $("#commentForm").valid(); 
    if (valid) { 
     $.post(
     'scripts/process_email_initial_form.php', 
     $(this).serialize(), 
     function(o) { 
      console.log('success'); 
     }, 
     'json') 
     .done(function(o) { 
      $(this).attr('action', 'success.php'); 
      return true; 
     }); 
    } else { 
     $('.full-error').show(); 
     return false; 
    } 
}); 

OR

$("#commentForm").submit(function(){ 
    var valid = $("#commentForm").valid(); 
    if (valid) { 
     $.post(
     'scripts/process_email_initial_form.php', 
     $(this).serialize(), 
     function(o) { 
      $(this).attr('action', 'success.php'); 
      return true; 
     }, 
     'json'); 
    } else { 
     $('.full-error').show(); 
     return false; 
    } 
});