How to validate and submit a form using AJAX? I've created a webform using the Form API. I'm using the #AJAX field option to add AJAX validation to each field.

Is it possible to validate and submit the form using AJAX without reloading the page. If validation fails I want to show an error message and if validation is successful then I want to show a message (ideally in a lightbox) and reset the form fields.

My code so far:

$form['name'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Name'),
    '#default_value' =&gt; '',
    '#maxlength' =&gt; '128',
    '#required' =&gt; TRUE,
    '#ajax' =&gt; array(
        'callback' =&gt; '_validate_name',
        'wrapper' =&gt; 'name-error-icon-container',
        'method' =&gt; 'html',
        'effect' =&gt; 'none',
        'progress' =&gt; array(
            'message' =&gt; NULL,
        ),
    ),
);

$form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Submit',
    '#ajax' =&gt; array(
        'callback' =&gt; '_handle_form_submit',
        'effect' =&gt; 'fade',
    ),
);


callback functions look like this:

function _validate_name($form, $form_state) {

    if ($form_state['values']['name'] != '') {
    $output = 'OK';
    }
    else {
      $output = 'Enter a value';
    }

   return $output;

}

function _handle_form_submit($form, $form_state) {
}


But I'm not sure what should go in the _handle_form_submit function to validate and then either return a message or submit form and reset fields?
I know this is question has been lying around for a couple of years by now, but I don't feel any of the other answers really get the point of Drupal 7 ajax form submission, so I'll try to explain it.
Since your form should work as well without the ajax as per good practice, your ajax submit handler should do nothing but return the form. Everything else should be in your validation and submit functions.
function ex_form($form, $form_state) {
  $form['name'] = array(
    '#type' =&gt; 'textfield',
     '#title' =&gt; t('Name'),
     '#default_value' =&gt; '',
     '#maxlength' =&gt; '128',
     '#required' =&gt; TRUE,
  );

  $form['submit'] = array(
   '#type' =&gt; 'submit',
   '#value' =&gt; 'Submit',
   '#ajax' =&gt; array(
     'callback' =&gt; 'ex_form_ajax_handler',
     'effect' =&gt; 'fade',
   ),
  );
}

function ex_form_submit(&amp;$form, &amp;$form_state) {
  // Submit logic - runs when validation is ok
  // You can alter the form here to be just a markup field if you want.
}

function ex_form_validate(&amp;$form, &amp;$form_state) {
  // Validation logic - invalidate using form_set_error() or form_error()
}

function ex_form_ajax_handler(&amp;$form, &amp;$form_state) {
  return $form;
}