it-swarm.com.de

Fügen Sie Felder hinzu, die sich dynamisch mit Ajax bilden, und rufen Sie Werte ab

Ich versuche, einem Formular dynamisch eine Kombination aus Textfeld und Auswahlfeld hinzuzufügen, indem ich Drupal 7 Ajax) verwende, wenn ich auf eine Schaltfläche klicke.

Aber ich habe hier zwei Probleme:

  1. Ich kann mit Ajax nicht mehr als einen Satz von Feldern in das Formular einfügen
  2. Nachdem ich das Formular gesendet habe, kann ich die Werte des Formulars nicht mehr abrufen. Sie sind leer. Ich habe die Felder "name_1", "select_1", "name_2", "select_2" usw.

Ich möchte wissen, ob es eine Möglichkeit gibt, es mit/ohne Verwendung eines Feldsatzes zu lösen.

Mein benutzerdefinierter Modulcode:

function my_form_form($form, &$form_state, $no_js_use = FALSE) {  
  $form['#tree'] = TRUE;
  $form['add_more'] = array(
    '#type' => 'button',
    '#value' => t('Add More'),
    '#href' => '',
    '#ajax' => array(
    'callback' => 'ajax_simplest_callback',
    'wrapper' => 'names-fieldset-wrapper',
  );
  $form['names']['name_1'] = array(
    '#type' => 'textfield', 
    '#title' => t('Name 1'),  
    '#attributes'=>array('id'=>'name_1', 'name'=>'name_1'),           
  );
  $form['names']['select_1'] = array(
    '#type' => 'select', 
    '#title' => t('Select 1'),    
    '#attributes'=>array('id'=>'select_1', 'name'=>'select_1'), 
    '#options'=>array('1'=>'One', '2'=>'Two', '3'=>'Three'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),  
  );
  $form['#submit'][] = 'my_form_add_more_submit';       
  return $form;
}
function ajax_simplest_callback($form, &$form_state) {
  if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
  }
  else {
    $form_state['num_names']++;
    $form_state['rebuild'] = TRUE;
  }

  for ($i = 1; $i < $form_state['num_names']; $i++) {       
    $form['names']['name_'.$i+1] = array(
      '#type' => 'textfield', 
      '#title' => t('Name '.$i+1),    
      '#attributes'=>array('id'=>'name_'.$i+1, 'name'=>'name_'.$i+1), 
    );    
    $form['names']['select_'.$i+1] = array(
      '#type' => 'select', 
      '#title' => t('Select '.$i+1),    
      '#attributes'=>array('id'=>'select_'.$i+1, 'name'=>'select_'.$i+1), 
      '#options'=>array('1'=>'One', '2'=>'Two', '3'=>'Three'),
    );
  }
  return $form['names'];
}

function my_form_add_more_submit($form, &$form_state) {
  print_r($form_state['values']);
  exit();
}
6
shasi kanth

Setzen Sie kein Feld #required zu wahr.

 $form['markup'] = array(
        '#type' => 'markup',
        '#prefix' => '<div id="my-wrapper">',
        '#suffix' => '</div>',
    );
 $form['markup']['myfield'] = array(
        '#type' => 'textfield', 
        '#size' => 12,
    );
1
Sumit Madan

Sie müssen Ihr Formular in Ihrer Formularfunktion neu erstellen, damit Ihre Werte in das Formular integriert werden. Sie möchten auch Feldsätze und deren Baumstruktur verwenden, um die Daten etwas besser zu organisieren.

function my_form_form($form, &$form_state, $no_js_use = FALSE) {

        $form['add_more'] = array(
          '#type' => 'button',
          '#value' => t('Add More'),
          '#ajax' => array(
            'callback' => 'ajax_simplest_callback',
            'wrapper' => 'replace-this',
            'method' => 'replace',
           ),
        );

        $form['names'] = array(
          '#type' => 'fieldset',
          '#title' => 'Names',
          '#prefix' => '<div id="replace-this">',
          '#suffix' => '</div>',
          '#tree' => TRUE,
        );

        // build the initial field if this is not being rebuilt 
        // from ajax request
        if (!array_key_exists('clicked_button', $form_state)) {
          $form['names'][0] = array('#type' => 'fieldset');
          $form['names'][0]['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name'),
            '#parent' => 'test',
          );
          $form['names'][0]['select'] = array(
            '#type' => 'select',
            '#title' => t('Select'),
            '#parent' => 'test',
            '#options'=>array('1'=>'One', '2'=>'Two', '3'=>'Three'),
          );
        } else {
          // otherwise add the fields for each existing value
          foreach ($form_state['input']['names'] as $i => $value) {
            $form['names'][$i] = array('#type' => 'fieldset');
            $form['names'][$i]['name'] = array(
              '#type' => 'textfield',
              '#title' => t('Name'),
              '#default_value' => $form_state['input']['names'][$i]['name'],
            );
            $form['names'][$i]['select'] = array(
              '#type' => 'select',
              '#title' => t('Select'),
              '#default_value' => $form_state['input']['names'][$i]['select'],
              '#options'=>array('1'=>'One', '2'=>'Two', '3'=>'Three'),
            );
          }

          // add the additional field for a new entry
          $last = count($form_state['input']['names']);
          $form['names'][$last] = array('#type' => 'fieldset');
          $form['names'][$last]['name'] = array(
              '#type' => 'textfield',
              '#title' => t('Name'),
          );
          $form['names'][$last]['select'] = array(
              '#type' => 'select',
              '#title' => t('Select'),
              '#options'=>array('1'=>'One', '2'=>'Two', '3'=>'Three'),
          );
        }

        $form['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Submit'),
        );
        $form['#submit'][] = 'my_form_add_more_submit';

        return $form;
    }

    function ajax_simplest_callback($form, &$form_state) {
        return $form['names'];
    }

    function my_form_add_more_submit($form, &$form_state) {
        print_r($form_state['values']);
        exit();
    }
1
thumbson