<div>Hi All</div>
<div>I have a small working custom module. Code is as below.</div>
<div> </div>
<div>It&#39;s a simple form. </div>
<div> </div>
<div>In one line I have 4 fields (company, start date, end date, checkbox).</div>
<div>Below I have &quot;add/remove more companies&quot; button.</div>
<div> </div>
<div>If user clicks &quot;add/remove more companies&quot; button, it adds one more line having company, start date, end date, checkbox fields.</div>
<div> </div>
<div>But in the process, it is loosing user entered data in previous line like company name, start date etc.</div>
<div> </div>
<div>Now the question is what logic / tricks I need to follow so that, I can keep user entered data in newly rendered form.</div>
<div> </div>
<div>Best Regards</div>
<div>Austin.</div>
<div> </div>
<div><br>&lt;?php</div>
<div>function resume_submit_menu() {<br> $items = array();<br> $items[&#39;resume_submit/form&#39;] = array(<br> &#39;title&#39; =&gt; t(&#39;Submit Your Resume&#39;),<br> &#39;page callback&#39; =&gt; &#39;resume_submit_form&#39;,<br>
 &#39;access arguments&#39; =&gt; array(&#39;access content&#39;),<br> &#39;type&#39; =&gt; MENU_CALLBACK,<br> );</div>
<div> $items[&#39;resume_submit/form/morecompanies&#39;] = array(<br> &#39;page callback&#39; =&gt; &#39;resume_submit_add_more_companies&#39;,<br> &#39;access arguments&#39; =&gt; array(&#39;access content&#39;),<br> &#39;type&#39; =&gt; MENU_CALLBACK,<br>
 );</div>
<div> return $items;<br>}</div>
<div>function resume_submit_form() {<br> return drupal_get_form(&#39;resume_submit_my_form&#39;);<br>}</div>
<div>function resume_submit_my_form($form_state) {<br> $form = array(&#39;#cache&#39; =&gt; TRUE,);<br> <br> if($form_state[&#39;values&#39;][&#39;numOfCompanies&#39;] == &#39;&#39;)<br> {<br>  $all_companies = 1;<br> }<br>
 else<br> {<br>  $all_companies = $form_state[&#39;values&#39;][&#39;numOfCompanies&#39;];<br> }</div>
<div> $form[&#39;numOfCompanies&#39;] = array(<br> &#39;#type&#39; =&gt; &#39;value&#39;,<br> &#39;#value&#39; =&gt; t($all_companies),<br> );<br> <br> //Add a wrapper for the companies and add more button.<br> $form[&#39;work_history_wrapper&#39;] = array(<br>
  &#39;#title&#39; =&gt; t(&quot;Work history: Mention all the companies you gave worked so far&quot;),<br>  &#39;#type&#39; =&gt; &#39;fieldset&#39;,<br>  &#39;#collapsible&#39; =&gt; TRUE,<br>  &#39;#collapsed&#39; =&gt; FALSE,<br>
  &#39;#prefix&#39; =&gt; &#39;&lt;div class=&quot;clear-block&quot; id=&quot;work-history-wrapper&quot;&gt;&#39;,<br>  &#39;#suffix&#39; =&gt; &#39;&lt;/div&gt;&#39;,<br> );</div>
<div> //Container for just companies<br> $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;] = array(<br>  &#39;#prefix&#39; =&gt; &#39;&lt;div id=&quot;all-companies&quot;&gt;&#39;,<br>  &#39;#suffix&#39; =&gt; &#39;&lt;/div&gt;&#39;,<br>
  &#39;#tree&#39; =&gt; TRUE,<br> );</div>
<div> //Need to do: Get the company name, start date and end date, pass it to resume_submit_add_one_company_form, so that it is shown properly<br> for($delta = 0; $delta &lt; $all_companies; $delta++)<br> {<br>  $company_name = isset($form_state[&#39;values&#39;][&#39;allcompanies&#39;][$delta][&#39;companies&#39;]) ? $form_state[&#39;values&#39;][&#39;allcompanies&#39;][$delta][&#39;companies&#39;] : &#39;&#39;;</div>

<div>  $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;][$delta][&#39;companies&#39;] = array(<br>   &#39;#type&#39; =&gt; &#39;textfield&#39;,<br>   &#39;#title&#39; =&gt; t(&#39;Company&#39;.&#39;/&#39;.($delta + 1)),<br>
   &#39;#parents&#39; =&gt; array(&#39;allcompanies&#39;, $delta, &#39;companies&#39;),<br>   &#39;#value&#39; =&gt; t($company_name),<br>   &#39;#size&#39; =&gt; 30,<br>   &#39;#maxlength&#39; =&gt; 50,<br>  );</div>
<div>  $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;][$delta][&#39;startdate&#39;] = array(<br>   &#39;#type&#39; =&gt; &#39;date&#39;,<br>   &#39;#title&#39; =&gt; t(&#39;Start Date&#39;),<br>   &#39;#parents&#39; =&gt; array(&#39;allcompanies&#39;, $delta, &#39;startdate&#39;),<br>
  );</div>
<div>  $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;][$delta][&#39;enddate&#39;] = array(<br>   &#39;#type&#39; =&gt; &#39;date&#39;,<br>   &#39;#title&#39; =&gt; t(&#39;End Date&#39;),<br>   &#39;#parents&#39; =&gt; array(&#39;allcompanies&#39;, $delta, &#39;enddate&#39;),<br>
  );</div>
<div>  $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;][$delta][&#39;removecompany&#39;] = array(<br>   &#39;#type&#39; =&gt; &#39;checkbox&#39;,<br>   &#39;#title&#39; =&gt; t(&#39;Check to remove&#39;),<br>
   &#39;#parents&#39; =&gt; array(&#39;allcompanies&#39;, $delta, &#39;removecompany&#39;),<br>  );  <br> }<br> <br> $form[&#39;work_history_wrapper&#39;][&#39;morecompany&#39;] = array(<br>  &#39;#type&#39; =&gt; &#39;submit&#39;,<br>
  &#39;#value&#39; =&gt; t(&#39;Add / Remove Companies&#39;),<br>  &#39;#weight&#39; =&gt; 1,<br>  &#39;#ahah&#39; =&gt; array(      <br>   &#39;path&#39; =&gt; &#39;resume_submit/form/morecompanies&#39;,      <br>   &#39;wrapper&#39; =&gt; &#39;all-companies&#39;,      <br>
   &#39;method&#39; =&gt; &#39;replace&#39;,      <br>  ),<br> );</div>
<div>$form[&#39;submit&#39;] = array(<br>&#39;#type&#39; =&gt; &#39;submit&#39;,<br>&#39;#value&#39; =&gt; &#39;Submit&#39;,<br>&#39;#submit&#39; =&gt; array(&#39;resume_submit_submit&#39;),<br>);</div>
<div>return $form;<br>}</div>
<div>function  resume_submit_add_more_companies() {</div>
<div> include_once &#39;modules/node/node.pages.inc&#39;;<br> $form_state = array(&#39;storage&#39; =&gt; NULL, &#39;submitted&#39; =&gt; FALSE);</div>
<div> $form_build_id = $_POST[&#39;form_build_id&#39;];<br> <br> // Get the form from the cache.<br> $form = form_get_cache($form_build_id, $form_state);<br> <br> $args = $form[&#39;#parameters&#39;];<br> $form_id = array_shift($args);<br>
 <br> // We will run some of the submit handlers so we need to disable redirecting.<br> $form[&#39;#redirect&#39;] = FALSE;<br> <br> // We need to process the form, prepare for that by setting a few internals<br> // variables.<br>
 $form[&#39;#post&#39;] = $_POST;<br> $form[&#39;#programmed&#39;] = FALSE;<br> $form_state[&#39;post&#39;] = $_POST;<br> <br> // Build, validate and if possible, submit the form.<br> drupal_process_form($form_id, $form, $form_state);</div>

<div> //drupal_set_message(t(&quot;3. Output is: &quot;).t($form_state[&#39;values&#39;][&#39;numOfCompanies&#39;]));<br> $form_state[&#39;values&#39;][&#39;numOfCompanies&#39;] = $form_state[&#39;values&#39;][&#39;numOfCompanies&#39;] + 1;<br>
 <br> // This call recreates the form relying solely on the form_state that the<br> // drupal_process_form set up.<br> $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);<br> <br> // Render the new output.<br>
 $choice_form = $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;];<br> <br> unset($choice_form[&#39;#prefix&#39;], $choice_form[&#39;#suffix&#39;]); // Prevent duplicate wrappers.<br> <br> $output = theme(&#39;status_messages&#39;) . drupal_render($choice_form);<br>
 drupal_json(array(&#39;status&#39; =&gt; TRUE, &#39;data&#39; =&gt; $output));<br> <br>}</div>
<div>function resume_submit_validate($form, &amp;$form_state) {<br>}<br> <br>function resume_submit_submit($form, &amp;$form_state) {<br>}</div>
<div>?&gt;<br></div>