Fix for A port of the popular "myform_sample" multiplication forms API

  1. <?php
  2.  
  3.  
  4.  
  5. /**
  6.  
  7. * A small example module of using the form api
  8.  
  9. * to display and process a form
  10.  
  11. * that is not used to extend the basic node content type
  12.  
  13. *
  14.  
  15. * In this sample the form collects two numbers
  16.  
  17. * and multiplies them together
  18.  
  19. */
  20.  
  21.  
  22.  
  23.  
  24.  
  25. /**
  26.  
  27. * Implementation of hook_menu().
  28.  
  29. *
  30.  
  31. */
  32.  
  33. function myform_menu($may_cache) {
  34.  
  35.   $items = array();
  36.  
  37.   $access = TRUE;
  38.  
  39.    
  40.  
  41.   if (!$may_cache) {
  42.  
  43.       // This determines the path used to show the form and also makes a menu entry
  44.  
  45.       // This first path is for the entry form
  46.  
  47.     $items[] = array('path' => 'myform/sample',
  48.  
  49.       'title' => t('sample form'),
  50.  
  51.       'callback' => 'myform_sampleform',
  52.  
  53.       'access' => $access);
  54.  
  55.      
  56.  
  57.     // This path is for the routine that actually does the multplication
  58.  
  59.     // It is only setup as a callback (no menu entry)
  60.  
  61.     $items[] = array('path' => 'myform/multiple',
  62.  
  63.       'title' => t('Multiplication Results'),
  64.  
  65.       'type' => MENU_CALLBACK,
  66.  
  67.       'callback' => myform_multiple,
  68.  
  69.       'access' => $access);
  70.  
  71.     }
  72.  
  73.    
  74.  
  75.   return $items;
  76.  
  77. }
  78.  
  79.  
  80.  
  81. function myform_sampleform() {
  82.  
  83.     $output = 'Hello!  Let\'s multiply some numbers, shall we?</br>';
  84.  
  85.     return $output . drupal_get_form('myform_sample');
  86.  
  87. }
  88.  
  89. function myform_sample() {
  90.  
  91.     $form = array();
  92.  
  93.    
  94.  
  95.     // Build up the form
  96.  
  97.     // See api.drupal.org/api/4.7/file/developer/topics/forms_api.html (quickstart guide)
  98.  
  99.     // and api.drupal.org/api/4.7/file/developer/topics/forms_api_reference.html (forms api)
  100.  
  101.     // for more information on creating forms
  102.  
  103.    
  104.  
  105.     $form['value1'] = array(
  106.  
  107.       '#type' => 'textfield',
  108.  
  109.       '#title' => t('Value 1'),
  110.  
  111.       '#size' => 4,
  112.  
  113.       '#maxlength' => 4,
  114.  
  115.       '#description' => t('The first value to perform the multiplication with.')
  116.  
  117.     );
  118.  
  119.    
  120.  
  121.     $form['value2'] = array(
  122.  
  123.       '#type' => 'textfield',
  124.  
  125.       '#title' => t('Value 2'),
  126.  
  127.       '#size' => 4,
  128.  
  129.       '#maxlength' => 4,
  130.  
  131.       '#description' => t('The second value to perform the multiplication with.')
  132.  
  133.     );
  134.  
  135.    
  136.  
  137.     // Make sure we have a submit button
  138.  
  139.  
  140.  
  141.     $form['submit'] = array('#type' => 'submit', '#value' => t('Multiple values'));
  142.  
  143.  
  144.  
  145.    
  146.  
  147.     // drupal_get_form produces the form
  148.  
  149.     // which return to the drupal system
  150.  
  151.     // which produces the page
  152.  
  153.    
  154.  
  155.     // The first parameter to drupal_get_form
  156.  
  157.     // is the form id.  It also determines the
  158.  
  159.     // default validation and submit function names
  160.  
  161.     //
  162.  
  163.     // In this case
  164.  
  165.     //    Validation: myform_sample_validate()
  166.  
  167.     //  Submit: my_form_submit()  
  168.  
  169.  
  170.  
  171.   return $form;
  172.  
  173. }
  174.  
  175. function myform_sample_validate($form_id, $form_values) {
  176.  
  177.     if ( !isset($form_values['value1']) ) {
  178.  
  179.         form_set_error('value1', t('You must provide the first multiplication value.'));
  180.  
  181.     }
  182.  
  183.     else if ( !is_numeric($form_values['value1']) ) {
  184.  
  185.         form_set_error('value1', t('The first multiplication value must be a number.'));
  186.  
  187.     }  
  188.  
  189.  
  190.  
  191.     if ( !isset($form_values['value2']) ) {
  192.  
  193.         form_set_error('value2', t('You must provide the second multiplication value.'));
  194.  
  195.     }
  196.  
  197.     else if ( !is_numeric($form_values['value2']) ) {
  198.  
  199.         form_set_error('value2', t('The second multiplication value must be a number.'));
  200.  
  201.     }  
  202.  
  203. }
  204.  
  205.  
  206.  
  207. function myform_sample_submit($form_id, $form_values) {
  208.  
  209.     // Submit routines do not directly produce any output
  210.  
  211.     // They do something with the form values
  212.  
  213.     // which is typically to store them in the database
  214.  
  215.     // then return a path to determine what page is shown next
  216.  
  217.    
  218.  
  219.     // In this case the path constructe includes the two values
  220.  
  221.    
  222.  
  223.     return 'myform/multiple/' . $form_values['value1'] . '/' . $form_values['value2'];
  224.  
  225. }
  226.  
  227.    
  228.  
  229.  
  230.  
  231. function myform_multiple($value1, $value2) {
  232.  
  233.     $output = '<p>';
  234.  
  235.     $output .= $value1. ' * ' . $value2 . ' = ' . ($value1 * $value2);
  236.  
  237.     $output .= '</p>';
  238.  
  239.    
  240.  
  241.     return $output;
  242.  
  243. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.