0
votes

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.

Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.

EDIT: I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.

Here is what I tried and it didn't work.

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

  $form = array(); 

  $form_state['rebuild'] = TRUE;  

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

  $form['render_area'] = array(
    '#type' => 'markup',
    '#markup' => '',
    '#prefix' => '<div id="job-agent-form">',
    '#suffix' => '</div>',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#attributes' => array('class' => array('submit')),
    '#ajax' => array(
      'callback' => '_display_form',
      'wrapper' => 'job-agent-form',
      'method' => 'replace',
      'effect' => 'fade',
    ),


  return $form;
}

function _display_form($form, &$form_state) {


  // there are other form elements that would go here also, I just added two for example

  $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,
  );

  return $form;
}
1
Why can't you use existing subscription modules ? - RajeevK
None of them worked the way we wanted them to without severe modification. Was just easier to create what we need from scratch. - Erich H.

1 Answers

0
votes

I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.