<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 &quot;Add More Categories&quot;.</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 &quot;$all_categories&quot; and form element $form[&#39;all_categories&#39;]  (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">&lt;?php</font></em></div>
<div><em><font color="#000099">function questionnaire_menu() {<br> $items = array();<br>  <br> $items[&#39;questionnaire/form/manage&#39;] = array(<br> &#39;title&#39; =&gt; t(&#39;Manage questions&#39;),<br> &#39;page callback&#39; =&gt; &#39;questionnaire_form&#39;,<br>
 &#39;access arguments&#39; =&gt; array(&#39;question manage&#39;),<br> &#39;type&#39; =&gt; 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>   &#39;question manage&#39; =&gt; array(<br>   &#39;title&#39; =&gt; t(&#39;Question management&#39;),<br>   &#39;description&#39; =&gt; t(&#39;Provides question management capability&#39;),<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(&#39;questionnaire_my_form&#39;);<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, &amp;$form_state) <br>{  <br>  $form_state[&#39;cache&#39;] = TRUE;<br>  <br>  $all_categories = !empty($form_state[&#39;values&#39;][&#39;all_categories&#39;]) ? $form_state[&#39;values&#39;][&#39;all_categories&#39;] : 1;  </font></em></div>

<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">  $form[&#39;all_categories&#39;] = array(<br>   &#39;#type&#39; =&gt; &#39;value&#39;, <br>   &#39;#default_value&#39; =&gt; $all_categories,<br>   &#39;#value&#39; =&gt; $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&#39;re replacing, named by    <br>   // #ajax[&#39;wrapper&#39;] above.    <br>   $form[&#39;category_fieldset&#39;] = array(    <br>        &#39;#title&#39; =&gt; t(&quot;Questions Categories&quot;),    <br>
        &#39;#prefix&#39; =&gt; &#39;&lt;div id=&quot;category-div&quot;&gt;&#39;,    <br>        &#39;#suffix&#39; =&gt; &#39;&lt;/div&gt;&#39;,    <br>        &#39;#type&#39; =&gt; &#39;fieldset&#39;,    <br>        &#39;#description&#39; =&gt; t(&#39;Please enter the question categories&#39;), <br>
   );</font></em></div>
<div><br><em><font color="#000099">   for ($i=1; $i&lt;= $all_categories ; $i++) <br>   {   <br>      $key = &#39;ctg&#39; . $i;<br>      $form[&#39;category_fieldset&#39;][&#39;category&#39;][$key] = array(      <br>         &#39;#type&#39; =&gt; &#39;textfield&#39;,      <br>
         &#39;#title&#39; =&gt; &quot;Question category $i&quot;,    <br>         );  <br>    }  </font></em></div>
<div><em><font color="#000099">   $form[&#39;category_fieldset&#39;][&#39;add_more_category&#39;] = array(    <br>        &#39;#type&#39; =&gt; &#39;submit&#39;,<br>        &#39;#value&#39; =&gt; t(&#39;Add More Categories&#39;),<br>
        &#39;#ajax&#39; =&gt; array(      <br>          &#39;callback&#39; =&gt; &#39;questionnaire_category_callback&#39;,<br>          &#39;wrapper&#39; =&gt; &#39;category-div&#39;,      <br>          &#39;method&#39; =&gt; &#39;replace&#39;,      <br>
          &#39;effect&#39; =&gt; &#39;fade&#39;,    <br>          ), <br>     );<br>    <br>    $form[&#39;submit&#39;] = array(    <br>        &#39;#type&#39; =&gt; &#39;submit&#39;,    <br>        &#39;#value&#39; =&gt; t(&#39;Submit&#39;),  <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, &amp;$form_state) <br>{ <br>  $form_state[&#39;values&#39;][&#39;all_categories&#39;] = $form_state[&#39;values&#39;][&#39;all_categories&#39;] + 1;<br>
  return $form[&#39;category_fieldset&#39;];<br>}</font></em></div>
<div><em><font color="#000099"></font></em> </div>
<div><em><font color="#000099">?&gt;</font></em></div>
<div> </div>