<div>Hi List</div>
<div>I am trying to learn ajax stuff in Drupal 7.</div>
<div> </div>
<div>I want to have a simple <font color="#808080"><font color="#808080">questionnaire</font></font> form. Where I will have a single textfield.</div>
<div>In that text field one will type a question category, and if he wants to type more question categories, user need to click a button "Add More Categories".</div>
<div>Please see link <a href="http://www.netcloudsystems.com/?q=questionnaire/form/manage">http://www.netcloudsystems.com/?q=questionnaire/form/manage</a></div>
<div> </div>
<div>The whole source code is at the end of this email. When ajax callback fires, I want to remember how many textfields I need to add, so that when a section of form is updated in _form() hook, I can according add those many textfields.</div>
<div> </div>
<div>I am trying to manage that using a variable "$all_categories" and form element $form['all_categories'] (of type value).</div>
<div> </div>
<div>I tried many options, always I find $all_categories value is 1. So I am unable to add more textfields. </div>
<div>My observation is whatever I set in ajax callback, in _form hook api, I find value is lost.</div>
<div> </div>
<div>Kindly guide me what I am missing.</div>
<div> </div>
<div>Best Regards</div>
<div>Kamal</div>
<div><strong>NECS, Bangalore</strong></div>
<div> </div>
<div> </div>
<div><em><font color="#000099"><?php</font></em></div>
<div><em><font color="#000099">function questionnaire_menu() {<br> $items = array();<br> <br> $items['questionnaire/form/manage'] = array(<br> 'title' => t('Manage questions'),<br> 'page callback' => 'questionnaire_form',<br>
'access arguments' => array('question manage'),<br> 'type' => MENU_SUGGESTED_ITEM,<br> );</font></em></div>
<div><em><font color="#000099"> return $items;<br>}</font></em></div>
<div><em><font color="#000099">function questionnaire_permission() <br>{<br> return array(<br> 'question manage' => array(<br> 'title' => t('Question management'),<br> 'description' => t('Provides question management capability'),<br>
),<br> ); <br>}</font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">function questionnaire_form() <br>{<br> return drupal_get_form('questionnaire_my_form');<br>}</font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">function questionnaire_my_form($form, &$form_state) <br>{ <br> $form_state['cache'] = TRUE;<br> <br> $all_categories = !empty($form_state['values']['all_categories']) ? $form_state['values']['all_categories'] : 1; </font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099"> $form['all_categories'] = array(<br> '#type' => 'value', <br> '#default_value' => $all_categories,<br> '#value' => $all_categories);</font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099"> // The prefix/suffix provide the div that we're replacing, named by <br> // #ajax['wrapper'] above. <br> $form['category_fieldset'] = array( <br> '#title' => t("Questions Categories"), <br>
'#prefix' => '<div id="category-div">', <br> '#suffix' => '</div>', <br> '#type' => 'fieldset', <br> '#description' => t('Please enter the question categories'), <br>
);</font></em></div>
<div><br><em><font color="#000099"> for ($i=1; $i<= $all_categories ; $i++) <br> { <br> $key = 'ctg' . $i;<br> $form['category_fieldset']['category'][$key] = array( <br> '#type' => 'textfield', <br>
'#title' => "Question category $i", <br> ); <br> } </font></em></div>
<div><em><font color="#000099"> $form['category_fieldset']['add_more_category'] = array( <br> '#type' => 'submit',<br> '#value' => t('Add More Categories'),<br>
'#ajax' => array( <br> 'callback' => 'questionnaire_category_callback',<br> 'wrapper' => 'category-div', <br> 'method' => 'replace', <br>
'effect' => 'fade', <br> ), <br> );<br> <br> $form['submit'] = array( <br> '#type' => 'submit', <br> '#value' => t('Submit'), <br>
); <br> <br> return $form;<br>} </font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">function questionnaire_category_callback($form, &$form_state) <br>{ <br> $form_state['values']['all_categories'] = $form_state['values']['all_categories'] + 1;<br>
return $form['category_fieldset'];<br>}</font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">?></font></em></div>
<div> </div>