Note that there are some explanatory texts on larger screens.

plurals
  1. PODrupal 7 Forms API - AJAX Forms error: An illegal choice has been detected. Please contact the site administrator
    primarykey
    data
    text
    <p>I have a form set up where a users selects an item from a drop down. Once that item is selected another drop down is populated. Then depending on the value that is selected from the second drop down a fieldset may or may not be shown. If the field set is shown there is a field and a button. By clicking the button you add another copy of the same field. Once there is more than one a remove one button shows up as well. I got the basis for the code from here: <a href="http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7" rel="nofollow">http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7</a></p> <p>The problem is when I click 'Add Another Survey Question' the first time it works fine and add a field. When I click it a second time or when I click remove now that there are two, I get this error: 'An illegal choice has been detected. Please contact the site administrator.'</p> <p>What am I doing wrong?</p> <p>Here's my code:</p> <pre><code>function touchpoints_addmetric_form($form, &amp;$form_state, $tp_id) { $selectedType = isset($form_state['values']['type']) ? $form_state['values']['type'] : FALSE; $types = db_query('SELECT * FROM {touchpoints_metric_types}') -&gt; fetchAllKeyed(0, 1); $form['#tree'] = TRUE; $form['type'] = array( '#type' =&gt; 'select', '#title' =&gt; t('Metric Type'), '#options' =&gt; $types, '#required'=&gt;TRUE, '#ajax' =&gt; array( 'event' =&gt; 'change', 'wrapper' =&gt; 'method-wrapper', 'callback' =&gt; 'touchpoints_method_callback' ) ); $form['measurementmethod'] = array( '#type' =&gt; 'select', '#title' =&gt; t('Measurement Method'), '#required'=&gt;TRUE, '#prefix' =&gt; '&lt;div id="method-wrapper"&gt;', '#suffix' =&gt; '&lt;/div&gt;', '#options' =&gt; _get_methods($selectedType) ); $form['survey'] = array(' #type' =&gt; 'fieldset', '#collapsible' =&gt; FALSE, '#states' =&gt; array( 'visible' =&gt; array( array( array(':input[name="measurementmethod"]' =&gt; array('value' =&gt; '5')), 'xor', array(':input[name="measurementmethod"]' =&gt; array('value' =&gt; '6')), 'xor', array(':input[name="measurementmethod"]' =&gt; array('value' =&gt; '7')) ) ) ) ); $form['survey']['contents'] = array( '#type' =&gt; 'fieldset', '#collapsible' =&gt; FALSE, '#prefix' =&gt; '&lt;div id="survey-div"&gt;', '#suffix' =&gt; '&lt;/div&gt;', ); if (empty($form_state['num_surveys'])) { $form_state['num_surveys'] = 1; } for ($i = 1; $i &lt;= $form_state['num_surveys']; $i++) { $form['survey']['contents']['survey_question'][$i] = array( '#type' =&gt; 'textfield', '#title' =&gt; t('Survey Question ' . $i), '#size' =&gt; 70, '#maxlength' =&gt; 100, ); } $form['survey']['contents']['addsurvey'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Add Another Survey Question'), '#submit' =&gt; array('touchpoints_metrics_survey_add_one'), '#limit_validation_errors' =&gt; array(), '#ajax' =&gt; array( 'callback' =&gt; 'touchpoints_metrics_survey_callback', 'wrapper' =&gt; 'survey-div', ), ); if ($form_state['num_surveys'] &gt; 1) { $form['survey']['contents']['removesurvey'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Remove A Survey Question'), '#submit' =&gt; array('touchpoints_metrics_survey_remove_one'), '#limit_validation_errors' =&gt; array(), '#ajax' =&gt; array( 'callback' =&gt; 'touchpoints_metrics_survey_callback', 'wrapper' =&gt; 'survey-div', ), ); } return $form; } function _get_methods($selected) { if ($selected) { $methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -&gt; fetchAllKeyed(0, 2); } else { $methods = array(); } return $methods; } function touchpoints_method_callback($form, &amp;$form_state) { return $form['measurementmethod']; } function touchpoints_metrics_survey_add_one($form, &amp;$form_state) { $form_state['num_surveys']++; $form_state['rebuild'] = TRUE; } function touchpoints_metrics_survey_remove_one($form, &amp;$form_state) { if ($form_state['num_surveys'] &gt; 1) { $form_state['num_surveys']--; } $form_state['rebuild'] = TRUE; } function touchpoints_metrics_survey_callback($form, &amp;$form_state) { return $form['survey']['contents']; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload