D6 FAPI Examples

  1. <?php
  2. // $Id: $
  3.  
  4. /**
  5.  * @file
  6.  * Makes a Form.
  7.  *
  8.  */
  9. function form_intro_menu() {
  10.  
  11.   $items = array();
  12.   $items['survey'] = array(
  13.     'title' => 'Super Sweet Survey',
  14.     'page callback' => 'survey_page',
  15.     'access arguments' => array('access content'),
  16.     'type' => MENU_SUGGESTED_ITEM,
  17.   );
  18.   return $items;
  19. }
  20.  
  21. function survey_page () {
  22.   return drupal_get_form('form_intro_form');
  23. }
  24.  
  25.  
  26.  
  27. function form_intro_form($form_state) {
  28.  
  29.   //figure out who the user is
  30.   global $user;
  31.   $name = $user->name;
  32.  
  33.  
  34.   $form['name'] = array(
  35.    '#type' => 'textfield',
  36.     '#title' => t('Your Name'),
  37.     '#size' => 60,
  38.     '#maxlength' => 150,
  39.     '#required' => TRUE,
  40.     '#default_value' => $name,
  41.   );
  42.  
  43.   $form['gender'] = array(
  44.     '#type' => 'radios',
  45.     '#title' => t('I am a...'),
  46.     '#options' => array(t('Male'), t('Female')),
  47. );
  48.  
  49.   $form['toast'] = array(
  50.     '#type' => 'checkbox',
  51.     '#title' => t('I Like Toast.'),
  52. );
  53.  
  54.   $form['submit'] = array(
  55.     '#type' => 'submit', '#value' => t('Submit'));
  56.  
  57.   return $form;
  58. }
  59.  
  60.  
  61. function form_intro_form_validate($form, &$form_state) {
  62.   if ($form_state['values']['name']=="Mr. T"){
  63.     form_set_error ('name', "I pitty the fool who thinks they are Mr. T");
  64.   }
  65. }
  66.  
  67.  
  68. function form_intro_form_submit($form_id, &$form_state) {
  69.   global $user;
  70.   $uid = $user->uid;
  71.   $name = $form_state['values']['name'];
  72.   $gender = $form_state['values']['gender'];
  73.   $toast = $form_state['values']['toast'];
  74.  
  75.   if (!$gender) {
  76.     $gender="Male";
  77.   }
  78.   else {
  79.     $gender="Female";
  80.   }
  81.  
  82.  
  83.   db_query("INSERT INTO {form_intro} (name, gender, toast, uid) VALUES ('%s', '%s', '%s', %d)",
  84.            $name, $gender, $toast, $uid);
  85.  
  86.   if ($toast) {
  87.     drupal_set_message("I like toast too. Thanks for the submission, friend.");
  88.   }
  89.   else {
  90.     drupal_set_message("The form has been submitted successfully.");
  91.   }
  92. }