Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function test_form($form_state = NULL) {
- ahah_helper_register($form, $form_state); // Absolutely vital for ahah_helper to do it's job
- // 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.
- if (!isset($form_state['storage']['quantity'])) {
- $quantity = 1;
- }
- else {
- // If the form has been submitted we get the quantity that was stored.
- $quantity = $form_state['storage']['quantity'];
- //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"
- if ($form_state['values']['op'] == 'submit') {
- //
- // db_query("INSERT INTO {ingredient} (Iname) VALUES ('%s')", $node->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' => '<div id="favourite-ingredients-wrapper">',
- '#suffix' => '</div>',
- '#tree' => TRUE // This is important for ahah_helper.
- );
- $form['Iname'] = array(
- '#type' => 'fieldset',
- '#title' => t('Ingredient(s)'),
- '#prefix' => '<div id="favourite-ingredients-wrapper">',
- '#suffix' => '</div>',
- '#tree' => TRUE // This is important for ahah_helper.
- );
- $form['ingredId'] = array(
- '#type' => 'fieldset',
- '#title' => t('IngredId(s)'),
- '#prefix' => '<div id="favourite-ingredients-wrapper">',
- '#suffix' => '</div>',
- '#tree' => TRUE // This is important for ahah_helper.
- );
- $form['weight'] = array(
- '#type' => 'fieldset',
- '#title' => t('Weight(s)'),
- '#prefix' => '<div id="favourite-ingredients-wrapper">',
- '#suffix' => '</div>',
- '#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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement