0

I want to get results, after validating and add fields as well in result like i want to add checkbox in result

function afaf_custom_form($form, &$form_state) {
  $form = array();

  $form['d_id'] = array(
    '#type' => 'textfield',
    '#title' => t('D id'),
    '#size' => 60,
    '#maxlength' => 60,
    '#required' => TRUE,

  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
    '#ajax' => array(
     'callback' => 'v_custom_ajax_handler',
     'wrapper' => 'display-dept-ajax',
     'method' => 'replace',
     'effect' => 'fade',
   ),
  );
$form["prefix"] = array(
    "#markup" => "<div id='display-dept-ajax'></div>");
$form["suffix"] = array(
    "#markup" => "</div>");



  return $form;
}



function v_custom_ajax_handler($form, &$form_state) {

  if (!empty($form_state['input']['d_id'])) {

  $form["department name"] = array(
    "#markup" => "<div id='display-dept-ajax'>Department Id : ""</div>

");


  return  $form["department name"];
  }
}
2

Basic working code example for validation or submit form with ajax.

 function afaf_custom_form($form, &$form_state) {
  $form['d_id'] = array(
    '#type' => 'textfield',
    '#title' => t('D id'),
    '#size' => 60,
    '#maxlength' => 60,
    '#required' => TRUE,

  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
    '#ajax' => array(
     'callback' => 'v_custom_ajax_handler',
     'wrapper' => 'display-dept-ajax',
   ),
  );

  $form['#prefix'] = '<div id="display-dept-ajax">';
  $form['#suffix'] = '</div>';
  if (isset($form_state['values'])) {
    $form['output'] = array('#markup' => 'thankyou message');
  }
  return $form;
}

function afaf_custom_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

function v_custom_ajax_handler($form, $form_state) {
  $form_errors = form_get_errors();
  if ($form_errors) {
    return $form;
  }
  else {
    $thank_you_html = drupal_render($form['output']);
    $commands[] = ajax_command_insert(NULL, $thank_you_html);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.