0

I am trying to create a form where initially, the user is presented with just one field, which is their email address. When they click submit, I would like it to be an AJAX submit where I then lookup their email in a custom table in the database and then present them with additional form fields that will either be blank if we do not have their email address in the sytem, or will be pre-populated and ready for them to edit, if we already have them in the system. I was thinking a multi step AJAX form might do the trick, but I don't know if it even needs to be "multi step". Can I accomplish this with just a standard AJAX submit? I just can't figure out how to show/populate those additional fields after they submit the form. This is what I have so far, I just can't figure out how to only show the additional fields AFTER the submit and verification process:

/**
 * Implements hook_form().
 */
function _vista_form($form, &$form_state) {

  $form = array();

  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'Email',
    '#required' => TRUE,
  );

  $form['wrapper'] = array(
    '#markup' => '',
    '#prefix' => '<div id="ajax_wrapper">',
    '#suffix' => '</div>',
  );


  // all of the fields below this are the ones that I would like to show ONLY after I look them up and then I can prefill them in or not
  $form['id'] = array(
    '#type' => 'hidden',
    '#value' => 0,    
  );

  $specialties = db_query("SELECT specialty_code, specialty_name FROM {specialties_vista}")->fetchAll();
  $default_specialties = explode('^', '');

  $specialty_options = array();
  foreach ($specialties as $specialty) {
    $specialty_options[$specialty->specialty_code] = $specialty->specialty_name;
  }

  $form['specialty'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#size' => 10,
    '#title' => 'Specialty',
    '#description' => 'Hold Control or Shift while clicking to select multiple specialties',
    '#options' => $specialty_options,    
    '#empty_option' => '-All-',
    '#empty_value' => 'all',
    '#default_value' => $default_specialties,
  );

  $type_options = array(
    'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
    'International' => 'International Locum Tenens',
    'Permanent' => 'Permanent Physician',
    'US-Locum-Tenens' => 'US Locum Tenens',
  );   

  $form['job_type'] = array(
    '#type' => 'checkboxes',
    '#multiple' => TRUE,
    '#title' => 'Type of Job',    
    '#options' => $type_options,    
    '#empty_option' => 'Choose a placement type',
    '#empty_value' => 'all',
    //'#default_value' => $type_selected,
  );  

  $form['active'] = array(
    '#type' => 'checkbox',    
    '#title' => 'Subscribe/Unsubscribe',    
    '#default_value' => 1,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#attributes' => array('class' => array('submit')),
    '#ajax' => array(
      'callback' => '_display_form',
      'wrapper' => 'ajax_wrapper',      
      'effect' => 'fade',
    ),
  );

  return $form;
}

function _display_form($form, $form_state) {
  //do database lookup here and set some variables? Not sure.
  return $form['wrapper'];
}
0

You need to keep track of the step you're in. The first time a user hit's the form you would initialize a variable, say $stepand give it the value 1. On submit you augment that value to be in step 2 on the form rebuild. In the form builder itself (_vista_form()) you add form items depending on the current step. The same goes for validation and submit handlers. Usually you would use switch statements.

Have a look at this very complete tutorial: http://www.jaypan.com/tutorial/drupal-7-ajax-driven-multi-step-forms-non-javascript-fallback

0

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.