Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a custom form with dynamically generated fields. I am unable to fetch values from these fields using form_state['values']. Code is attached below. I have been struggling with this since two days, but have not found anything that can fix the issue. Please help!!!

function oulta_seller_create_search_agent_form_second($form, &$form_state) {


$form['#tree'] = TRUE;

$form['names_fieldset'] = array(
  '#type' => 'fieldset',
  '#title' => t('People coming to the picnic'),
  '#prefix' => '<div id="names-fieldset-wrapper">',
  '#suffix' => '</div>',
);

if (empty($form_state['num_names'])) {
  $form_state['num_names'] = 1;
}
for ($i = 0; $i < $form_state['num_names']; $i++) {
  $form['names_fieldset']['name'][$i] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
);
if(!empty($form_state['values'][$i]) &&  ($form_state['values'][$i] !='')) {
drupal_set_message($form_state['values'][$i]);//<-- This statement never gets executed. Also cannot fetch the value of $form_state['values'][$i] 
}


}
 $form['names_fieldset']['add_name'] = array(
  '#type' => 'submit',
  '#value' => t('Add one more'),
  '#submit' => array('ajax_example_add_more_add_one'),
  '#ajax' => array(
  'callback' => 'ajax_example_add_more_callback',
  'wrapper' => 'names-fieldset-wrapper',
),
);
if ($form_state['num_names'] > 1) {
  $form['names_fieldset']['remove_name'] = array(
    '#type' => 'submit',
    '#value' => t('Remove one'),
    '#submit' => array('ajax_example_add_more_remove_one'),
    '#ajax' => array(
      'callback' => 'ajax_example_add_more_callback',
      'wrapper' => 'names-fieldset-wrapper',
  ),
);
}
$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
);


return $form;
}

function ajax_example_add_more_add_one($form, &$form_state) {
 $form_state['num_names']++;
 $form_state['rebuild'] = TRUE;
}

function ajax_example_add_more_callback($form, $form_state) {
 return $form['names_fieldset'];
}

function ajax_example_add_more_remove_one($form, &$form_state) {
 if ($form_state['num_names'] > 1) {
   $form_state['num_names']--;
 }
 $form_state['rebuild'] = TRUE;
}
share|improve this question

1 Answer

You have used $form_state['num_names'] instead of $form_state['values']['num_names'].

Please try to update and see.

share|improve this answer
 
$form_state['num_names'] is like a counter for the number of text fields. The referred code above is not incorrect. Thanks for trying. –  RJetty Aug 23 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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