Iname); } // 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. if (isset($form_state['values']['Iname']['add_more']) && $form_state['values']['op'] == 'Add ingredients' || $form_state['values']['op'] == 'Add More Ingredients') { $quantity++; } } // We create a hidden form element to store the amount of fields that the user has added. $form['quantity'] = array( '#type' => 'value', '#value' => $quantity, ); $form['Rname'] = array( '#type' => 'textfield', '#title' => t('Recipe Name'), '#size' => 36, '#default_value' => $form_state['values']['Rname'], ); $form['recipeId'] = array( '#type' => 'textfield', '#title' => t('Recipe Id'), '#size' => 36, '#default_value' => $form_state['values']['recipeId'], ); $form['method'] = array( '#type' => 'textfield', '#title' => t('Preparation'), '#size' => 110, '#default_value' => $form_state['values']['method'], '#prefix' => '
', '#suffix' => '
', '#tree' => TRUE // This is important for ahah_helper. ); $form['Iname'] = array( '#type' => 'fieldset', '#title' => t('Ingredient(s)'), '#prefix' => '
', '#suffix' => '
', '#tree' => TRUE // This is important for ahah_helper. ); $form['ingredId'] = array( '#type' => 'fieldset', '#title' => t('IngredId(s)'), '#prefix' => '
', '#suffix' => '
', '#tree' => TRUE // This is important for ahah_helper. ); $form['weight'] = array( '#type' => 'fieldset', '#title' => t('Weight(s)'), '#prefix' => '
', '#suffix' => '
', '#tree' => TRUE // This is important for ahah_helper. ); // We now do a simple loop, creating however many textfields are defined by $form_state['storage']['quantity'] for ($i = 1; $i <= $quantity; $i++) { // The element name needs to be different for each textfield, otherwise we will only get one value after the form is submitted. $form['Iname']['Iname'. $i] = array( '#type' => 'textfield', '#title' => t('Ingredient'), '#size' => 36, '#default_value' => $form_state['values']['Iname']['Iname'. $i], ); $form['ingredId']['ingredId_'. $i] = array( '#type' => 'textfield', '#title' => t('IngredId'), '#size' => 36, '#default_value' => $form_state['values']['ingredId']['IngredId_'. $i], ); $form['weight']['weight_'. $i] = array( '#type' => 'textfield', '#title' => t('weight'), '#size' => 36, '#default_value' => $form_state['values']['weight']['weight_'. $i], ); } // We add in a button with the #ahah element which will handle all our work for us. $form['weight']['add_more'] = array( '#type' => 'submit', '#value' => t('Add More Ingredients'), '#ahah' => array( 'event' => 'click//', // When the button is "clicked", AHAH will do it's job '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'). 'wrapper' => 'favourite-ingredients-wrapper', // We then define the wrapper which will be changed. ) ); $form['submit'] = array( '#type' => 'submit', '#value' => t('submit'), '#ahah' => array( 'event' => 'click', 'path' => ahah_helper_path(array('Iname')), 'wrapper' => 'favourite-ingredients-wrapper', ), ); return $form; }