Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The beginning of your post is confusing: your sentence states "While onchange the state the cities should be viewed in check boxes. Below is my code" ~ the states should be checkboxes or a select?</p> <p>Either way, I have this as working code with states as a 'select' and cities as 'checkboxes':</p> <pre><code>function state_form($form, &amp;$form_state) { $form = array(); $form['rate_the_service']['state'] = array( '#type' =&gt; 'select', '#prefix'=&gt;'&lt;div id="dropdown-third-replace"&gt;', '#suffix'=&gt;'&lt;/div&gt;', '#options'=&gt;array( '0' =&gt; t('Ohio'), '1' =&gt; t('New York'), ), '#validated' =&gt; TRUE, '#ajax' =&gt; array( 'event' =&gt; 'change', 'callback' =&gt; 'ajax_example_dependent_dcheck_state_callback', 'wrapper' =&gt; 'checkboxes-four-replace', ), ); $city_array = array( '1' =&gt; array( '1' =&gt; 'Cincinnati', '2' =&gt; 'Cleveland', '3' =&gt; 'Toledo' ), '2' =&gt; array( '4' =&gt; 'New York City', '5' =&gt; 'Buffalo', '6' =&gt; 'Syracuse' ), ); if (isset($form_state['values']['state']) &amp;&amp; $form_state['values']['state'] == 1) { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'checkboxes', '#options' =&gt; $city_array[2], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('1'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } else { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'checkboxes', '#options' =&gt; $city_array[1], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('2'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } return $form; } /** * Ajax Callback for development_form */ function ajax_example_dependent_dcheck_state_callback($form, $form_state) { return $form['rate_the_service']['city']; } </code></pre> <p>Optionally, if you wanted state to be 'checkboxes' as well as cities, you could do this:</p> <pre><code>function state_form($form, &amp;$form_state) { $form = array(); $form['rate_the_service']['state'] = array( '#type' =&gt; 'checkboxes', '#prefix'=&gt;'&lt;div id="dropdown-third-replace"&gt;', '#suffix'=&gt;'&lt;/div&gt;', '#options'=&gt;array( 'ohio' =&gt; t('Ohio'), 'new_york' =&gt; t('New York'), ), '#validated' =&gt; TRUE, '#ajax' =&gt; array( 'event' =&gt; 'change', 'callback' =&gt; 'ajax_example_dependent_dcheck_state_callback', 'wrapper' =&gt; 'checkboxes-four-replace', ), ); $city_array = array( '1' =&gt; array( '1' =&gt; 'Cincinnati', '2' =&gt; 'Cleveland', '3' =&gt; 'Toledo' ), '2' =&gt; array( '4' =&gt; 'New York City', '5' =&gt; 'Buffalo', '6' =&gt; 'Syracuse' ), ); // refine as needed to fit your validation, if this is your use case if (isset($form_state['values']['state']['ohio']) &amp;&amp; $form_state['values']['state']['ohio'] == 'ohio') { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'checkboxes', '#options' =&gt; $city_array[1], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('1'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } else if (isset($form_state['values']['state']['new_york']) &amp;&amp; $form_state['values']['state']['new_york'] == 'new_york') { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'checkboxes', '#options' =&gt; $city_array[2], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('2'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } else { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'checkboxes', '#options' =&gt; array(), '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('2'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } return $form; } /** * Ajax Callback for development_form */ function ajax_example_dependent_dcheck_state_callback($form, $form_state) { return $form['rate_the_service']['city']; } </code></pre> <p>and here is the case with states as a 'select' box and cities as a 'select' box</p> <pre><code>function state_form($form, &amp;$form_state) { $form = array(); $form['rate_the_service']['state'] = array( '#type' =&gt; 'select', '#prefix'=&gt;'&lt;div id="dropdown-third-replace"&gt;', '#suffix'=&gt;'&lt;/div&gt;', '#options'=&gt;array( 'ohio' =&gt; t('Ohio'), 'new_york' =&gt; t('New York'), ), '#validated' =&gt; TRUE, '#ajax' =&gt; array( 'event' =&gt; 'change', 'callback' =&gt; 'ajax_example_dependent_dcheck_state_callback', 'wrapper' =&gt; 'checkboxes-four-replace', ), ); $city_array = array( '1' =&gt; array( '1' =&gt; 'Cincinnati', '2' =&gt; 'Cleveland', '3' =&gt; 'Toledo' ), '2' =&gt; array( '4' =&gt; 'New York City', '5' =&gt; 'Buffalo', '6' =&gt; 'Syracuse' ), ); if (isset($form_state['values']['state']) &amp;&amp; $form_state['values']['state'] == 'new_york') { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'select', '#options' =&gt; $city_array[2], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('1'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } else { $form['rate_the_service']['city'] = array( '#prefix'=&gt;'&lt;div id="checkboxes-four-replace"&gt;', '#type' =&gt; 'select', '#options' =&gt; $city_array[1], '#default_value' =&gt; isset($values['city']) ? $values['city'] : array('2'), '#suffix'=&gt;'&lt;/div&gt;&lt;/div&gt;', ); } return $form; } /** * Ajax Callback for development_form */ function ajax_example_dependent_dcheck_state_callback($form, $form_state) { return $form['rate_the_service']['city']; } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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