Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

drupalHelp

By: a guest on Mar 23rd, 2011  |  syntax: PHP  |  size: 4.52 KB  |  views: 167  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3.  
  4. function test_form($form_state = NULL) {
  5.  
  6. ahah_helper_register($form, $form_state); // Absolutely vital for ahah_helper to do it's job
  7.  
  8. // We check to see if quantity has been set, ie if the form has been submitted yet. If not, we show 1 field, you can change this to however many fields you want to show up as default.
  9.         if (!isset($form_state['storage']['quantity'])) {
  10.                 $quantity = 1;
  11.         }
  12.         else {
  13. // If the form has been submitted we get the quantity that was stored.
  14.                 $quantity = $form_state['storage']['quantity'];
  15.  
  16.                
  17.                
  18. //the below if statement is where im having trouble and that is the last attempt I made at inserting into my database ingredient in column "Iname"             
  19.                
  20.                
  21.                
  22.                 if ($form_state['values']['op'] == 'submit') {
  23. //
  24. //              db_query("INSERT INTO {ingredient} (Iname) VALUES ('%s')", $node->Iname);
  25.                        
  26.                 }
  27. // We then do a check to see if the user has clicked the "Add ingredient" button, if they have we increase the amount by 1.
  28.         if (isset($form_state['values']['Iname']['add_more']) && $form_state['values']['op'] == 'Add ingredients' || $form_state['values']['op'] == 'Add More Ingredients') {
  29.                         $quantity++;
  30.                 }
  31.         }
  32.  
  33. // We create a hidden form element to store the amount of fields that the user has added.
  34.                 $form['quantity'] = array(
  35.                 '#type' => 'value',
  36.                 '#value' => $quantity,
  37.         );
  38.  
  39.  
  40.  
  41.  
  42.         $form['Rname'] = array(
  43.                 '#type' => 'textfield',
  44.                 '#title' => t('Recipe Name'),
  45.                 '#size' => 36,
  46.                 '#default_value' => $form_state['values']['Rname'],
  47.         );
  48.        
  49.        
  50.         $form['recipeId'] = array(
  51.                 '#type' => 'textfield',
  52.                 '#title' => t('Recipe Id'),
  53.                 '#size' => 36,
  54.                 '#default_value' => $form_state['values']['recipeId'],
  55.         );
  56.        
  57.         $form['method'] = array(
  58.                 '#type' => 'textfield',
  59.                 '#title' => t('Preparation'),
  60.                 '#size' => 110,
  61.                 '#default_value' => $form_state['values']['method'],
  62.                 '#prefix' => '<div id="favourite-ingredients-wrapper">',
  63.                 '#suffix' => '</div>',
  64.                 '#tree' => TRUE // This is important for ahah_helper.
  65.         );
  66.  
  67.        
  68.         $form['Iname'] = array(
  69.                 '#type' => 'fieldset',
  70.                 '#title' => t('Ingredient(s)'),
  71.                 '#prefix' => '<div id="favourite-ingredients-wrapper">',
  72.                 '#suffix' => '</div>',
  73.                 '#tree' => TRUE // This is important for ahah_helper.
  74.         );
  75.        
  76.        
  77.         $form['ingredId'] = array(
  78.                 '#type' => 'fieldset',
  79.                 '#title' => t('IngredId(s)'),
  80.                 '#prefix' => '<div id="favourite-ingredients-wrapper">',
  81.                 '#suffix' => '</div>',
  82.                 '#tree' => TRUE // This is important for ahah_helper.
  83.         );
  84.        
  85.         $form['weight'] = array(
  86.                 '#type' => 'fieldset',
  87.                 '#title' => t('Weight(s)'),
  88.                 '#prefix' => '<div id="favourite-ingredients-wrapper">',
  89.                 '#suffix' => '</div>',
  90.                 '#tree' => TRUE // This is important for ahah_helper.
  91.         );
  92.        
  93.        
  94. // We now do a simple loop, creating however many textfields are defined by $form_state['storage']['quantity']
  95.                 for ($i = 1; $i <= $quantity; $i++) {
  96. //              The element name needs to be different for each textfield, otherwise we will only get one value after the form is submitted.
  97.                         $form['Iname']['Iname'. $i] = array(
  98.                                 '#type' => 'textfield',
  99.                                 '#title' => t('Ingredient'),
  100.                                 '#size' => 36,
  101.                                 '#default_value' => $form_state['values']['Iname']['Iname'. $i],
  102.                 );
  103.                
  104.                         $form['ingredId']['ingredId_'. $i] = array(
  105.                                 '#type' => 'textfield',
  106.                                 '#title' => t('IngredId'),
  107.                                 '#size' => 36,
  108.                                 '#default_value' => $form_state['values']['ingredId']['IngredId_'. $i],        
  109.                 );
  110.                
  111.                         $form['weight']['weight_'. $i] = array(
  112.                                 '#type' => 'textfield',
  113.                                 '#title' => t('weight'),
  114.                                 '#size' => 36,
  115.                                 '#default_value' => $form_state['values']['weight']['weight_'. $i],            
  116.                 );
  117.         }
  118.        
  119.        
  120.        
  121.        
  122.  
  123. // We add in a button with the #ahah element which will handle all our work for us.
  124.        
  125.                 $form['weight']['add_more'] = array(
  126.                         '#type' => 'submit',
  127.                         '#value' => t('Add More Ingredients'),
  128.                         '#ahah' => array(
  129.                         'event' => 'click//', // When the button is "clicked", AHAH will do it's job
  130.                         'path' => ahah_helper_path(array('weight')), // The array features the wrapper form field. So our form wrapper is $form['ingredients'], so we set this to array('ingredients'). If your form was $form['ingredients']['another_wrapper'], the path would be array('ingredients', 'another_wrapper').
  131.                         'wrapper' => 'favourite-ingredients-wrapper', // We then define the wrapper which will be changed.
  132.                 )
  133.         );
  134.        
  135.         $form['submit'] = array(
  136.                 '#type' => 'submit',
  137.                 '#value' => t('submit'),
  138.                 '#ahah' => array(
  139.                 'event' => 'click',
  140.                 'path' => ahah_helper_path(array('Iname')),
  141.                 'wrapper' => 'favourite-ingredients-wrapper',
  142.                 ),
  143.         );
  144.         return $form;
  145. }