2

I am new to AJAX.

I am trying to submit a form with ajax using the following logic:

//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)

My 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;
});

I was wondering if I am on the right track with this implementation? or if this implementation is even possible?

I greatly appreciate any advice in making this implementation work.

Many thanks in advance!

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • You are on the right track, but you need to get your form data in to a javascript variable to use with you [`$.post`](http://api.jquery.com/jQuery.post/) and add `data: myFormData` – Jon Apr 07 '13 at 18:15

1 Answers1

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;
});
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189