Friday, 2 October 2015

How to validate and submit a form using AJAX in Drupal ?

Drupal convert your form in to ajax 
Below is the example of ajaxifying your drupal form. all you have to do is write the call back
wrapper Form api will ajaxify your form.  
function test_form($form, &$fstate) {
  $form["wrapper"] = array("#markup" => "<div id='test-ajax'></div>");
  $form["name"] = array("#type" => "textfield", "#required" => true, "#title" => "Name");
  $form["submit"] = array(
    "#type" => "submit", 
    "#value" => "Send", 
    "#ajax" => array(
    "callback" => "test_form_callback", 
    "wrapper" => "test-ajax",
    "effect" => "fade"));
  return $form;

function test_form_callback($form, &$fstate) {
  return "<div id='test-ajax'>Wrapper Div</div>";

function form_validate($form, &$fstate) {
  form_set_error("name", "your  error to display.");
Reference Links:

No comments:

Post a Comment