In my attempt to learn Drupal, I have created a module that allows for users to sign up for events. I have a problem with understanding just how exactly i make Drupal receive a signup and store it in the database.

What I have done so far:

  • Created the module with the required .module, .info and .install files, plus some css and stuff.
  • The module installs (and uninstalls) correctly, and the database tables for my module are created (namely the events table and the signup table).
  • I can add events and signups manually via eg. phpMyAdmin.

I have also managed to set up a form for signing up, but when I press "Sign up", nothing is stored. Where do I "connect" the mymodule_submit function in all of this?

My code: (please correct me if it is insecure or "bad form" or anything)

The form:

function krkhwebcal_signup() { 
  $getstring = explode("/",$_GET['q']);
  /**
  * the array values are these:
  * [0]: krkhwebcal – no use for the calendar output
  * [1]: [year] – this sets the year the calendar should display
  * [2]: [month] – this sets the month the calendar should display
  * [3]: [day] – this sets the day the calendar should mark as selected
  * [4]: [event_id] – if it exists, this indicates the id of any event we have asked for
  */
  // sanitize the input by making it integers
  $year = (int)$getstring[1];
  $month = (int)$getstring[2];
  $day = (int)$getstring[3];
  $event = (int)$getstring[4];

  $form['signup'] = array(
    '#type' => 'fieldset',
    '#title' => t('Signup'),
  );
  $form['signup']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#size' => '30',
    '#maxlength' => '50',
  );
  $types = array('regular', 'car', 'boatatv', 'other');
  $form['signup']['crew_type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => $types,
  );
  $form['signup']['notice'] = array(
    '#type' => 'textarea',
    '#title' => t('Notice'),
    '#cols' => 30,
    '#rows' => 3,
  );
  $form['signup']['year'] = array('#type' => 'hidden', '#value' => $year);
  $form['signup']['month'] = array('#type' => 'hidden', '#value' => $month);
  $form['signup']['day'] = array('#type' => 'hidden', '#value' => $day);
  $form['signup']['eid'] = array('#type' => 'hidden', '#value' => $event);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Sign up'));
  return $form;
} // function krkhwebcal_signup

The _submit function:

function krkhwebcal_submit($form, &$form_state) {
  db_query("INSERT INTO ".$db_prefix."krkhwebcal_signups (eid, name, crew_type, notice) 
    VALUES (%d, '%s', '%s', '%s')", 
    $form_state['values']['eid'], 
    $form_state['values']['name'], 
    $form_state['values']['crew_type'], 
    $form_state['values']['notice']);
  $form_state['redirect'] = '/krkhwebcal/';
  $form_state['redirect'] .= $form_state['values']['year'].'/';
  $form_state['redirect'] .= $form_state['values']['month'].'/';
  $form_state['redirect'] .= $form_state['values']['day'].'/';
  $form_state['redirect'] .= $form_state['values']['eid'];
  drupal_set_message(t('You have been signed up.'));
} // function krkhwebcal_submit

Comments

krishnarp’s picture

Hi

Change your submit function name to "function krkhwebcal_signup_submit()"

Regards
Krishna

tjodolv’s picture

That worked, thanks a lot :)