0

This form creates dynamic fields. I am trying to build a submit handler for each of the dynamic field values. So for each of the $form['items_fieldset'][$i]['serial'], (amount, and description) I could have many fields. How would I pass those to my database?

Here's what I have thusfar:

Form excerpt:

$form['#tree'] = TRUE;

$form['items_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Shippment Inventory Items:'),
'#prefix' => '<div id="items-fieldset-wrapper">',
'#suffix' => '</div>',
); 

 if (empty($form_state['num_items'])) {
$form_state['num_items'] = 1;
} 


 for ($i = 0; $i < $form_state['num_items']; $i++) {

$form['items_fieldset'][$i] = array(
'#prefix' => '<div class="two-col">',
'#suffix' => '</div>'
);

$form['items_fieldset'][$i]['amount'] = array(
  '#type' => 'textfield',
  '#size' => 30,
  '#title' => t('Amount:'),
  '#required' => FALSE,
  '#prefix' => '<div style="float:left; padding-right: 50px;">',
  '#suffix' => '</div>',
);

$form['items_fieldset'][$i]['description'] = array(
  '#type' => 'textfield',
  '#size' => 50,
  '#title' => t('Description:'),
  '#required' => FALSE,
  '#prefix' => '<div style="float:left; padding-right: 50px;">',
  '#suffix' => '</div>',
); 

$form['items_fieldset'][$i]['serial'] = array(
  '#type' => 'textfield',
  '#size' => 30,
  '#title' => t('Serial Number:'),
  '#required' => FALSE,
  '#prefix' => '<div style="float:left;">',
  '#suffix' => '</div><div style="clear:both;"></div>',
);
} 

$form['items_fieldset']['add_items'] = array(
'#type' => 'submit',
'#value' => t('Add more'),
'#submit' => array('_nnreport_shipping_new_add_one'),
'#ajax' => array(
    'callback' => '_nnreport_shipping_new_callback',
    'wrapper' => 'items-fieldset-wrapper',
    ),
); 

Handler to pass to database (not including all the static form values):

 function _nnreport_shipping_new_submit($form, &$form_state) {
    $submission = new stdClass();
    //This doesnt work, but I think it's kinda what I'm after...?? 
     for ($i = 0; $i < $form_state['num_items']; $i++) {
          $submission=>amount.$i = $form_state['values']['$i']['amount'];

          };
          _nnreport_shipping_new_update($submission); }

After that I submit the values of $submission to the database, like this:

function _nnreport_shipping_new_update(&$submission){

     db_insert('nnj_shipment')
        ->fields(array(
            'hotshotId' => $submission->carrier,
            'driver' => $submission->driver,
            'driverPhone' => $submission->driverPhone,
             //lots more of those...
              ))
             ->execute();

I've unsuccessfully tried a few variations to get the dynamic fields into the object...I would be very grateful for someone to point me in the correct direction.

0

Well after ignoring this problem for a while it was time to find an answer.

The issue was with using $form['#tree'] = TRUE; I didn't use the full array references after that...eg: $form_state[$i]['description'] needed to be $form_state['values']['items_fieldset'][$i]['description'] = 'foo'; (

After that I just used a foreach to grab my values from the dynamic field values...like: foreach ($form_state['values']['items_fieldset'] as $v){ $submission->description[] = $v['description']; }

And last was to dump them into my DB, and life is good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.