2

我是 AJAX 的新手。

我正在尝试使用以下逻辑提交带有 ajax 的表单:

//validate form

//if invalid never submit
//if valid, ajax request to get msg
   //get success html if no server side errors exist. Replace sign up page with success html
   //get fail message if server side errors exist, so the user can correct them (ex. invalid credit card no)

我的 AJAX:

$("form").submit(function(e){
   if($(this).valid()) {

      $.ajax({
        url: 'formAction.php',
        type: 'POST',
        success: function(data){
           /*convert php variables to JavaScript ones*/
            if(success==true){
                 //relpace current page with confirmation
            }
            if(success==false){
                 //print out errors and allow user to re-submit
            }
        }                        
    });         

    }
    e.preventDefault;
});

我想知道我是否在这个实现的正确轨道上?或者如果这个实现是可能的?

我非常感谢任何有关使此实施工作的建议。

提前谢谢了!

4

1 回答 1

3
$("form").submit(function(e){
   if($(this).valid()) {

      $.ajax({
        url: 'formAction.php',
        data: $(this).serialize(), //You need to provide this, serialize may work here
        type: 'POST', //Added single quote to prevent unterminated string literal
        success: function(data){
           //call was successful since success message was called
        },//Don't forget closing curly brace on function
        error: function(data){
           //Specify a call back for errors
        }                        
    });         

    }
    e.preventDefault;
});
于 2013-04-07T18:15:48.533 回答