<div>Hi All</div>
<div>I am quite new to AHAH, strugling to find how it works. I referred documentation and poll module.</div>
<div>I want 3 fields in a fieldset, <strong>Company, Start-Date, End-Date</strong>.  </div>
<div> </div>
<div>I have a button &quot;<strong>Add More Company</strong>&quot;, if clicked, it should add a new row having <strong>Company, Start-Date, End-Date </strong>dynamically.</div>
<div>How many times user clicks, those many rows needs to be added.</div>
<div> </div>
<div>To get the above mentioned form, I have put the code as below.</div>
<div>However, on button click, new rows are not adding up.</div>
<div> </div>
<div>Please help me to find whats the error with code.</div>
<div> </div>
<div>Regards,</div>
<div>Austin</div>
<div> </div>
<div>Code</div>
<div>===============================================</div>
<div>&lt;?php</div>
<div>function submitresume_menu() {<br> $items = array();<br> $items[&#39;submitresume/form&#39;] = array(<br> &#39;title&#39; =&gt; t(&#39;Submit Your Resume&#39;),<br> &#39;page callback&#39; =&gt; &#39;submitresume_form&#39;,<br>
 &#39;access arguments&#39; =&gt; array(&#39;access content&#39;),<br> &#39;description&#39; =&gt; t(&#39;Submit Your Resume&#39;),<br> &#39;type&#39; =&gt; MENU_CALLBACK,<br> );</div>
<div> $items[&#39;submitresume/form/morecompanies&#39;] = array(<br> &#39;page callback&#39; =&gt; &#39;submitresume_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 submitresume_form() {<br> return drupal_get_form(&#39;submitresume_my_form&#39;);<br>}</div>
<div>function submitresume_my_form($form_state) {</div>
<div> $form = array(&#39;#cache&#39; =&gt; TRUE,);</div>
<div> if (isset($form_state[&#39;all_companies&#39;])) {<br>  $all_companies = $form_state[&#39;all_companies&#39;];<br> }<br> else {<br>  $all_companies = 1;<br> }</div>
<div> //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>
 );</div>
<div> for ($delta = 0; $delta &lt; $all_companies; $delta++) <br> {<br>  //Need to do: Get the company name, start date and end date, pass it to submitresume_add_one_company_form, so that it is shown properly<br>  $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;][$delta] = submitresume_add_one_company_form($delta, $company_name, $compsdt, $compedt);<br>
 }</div>
<div> $form[&#39;work_history_wrapper&#39;][&#39;morecompany&#39;] = array(<br>  &#39;#type&#39; =&gt; &#39;button&#39;,<br>  &#39;#value&#39; =&gt; t(&#39;Add More Companies&#39;),<br>  &#39;#weight&#39; =&gt; 1,<br>  &#39;#submit&#39; =&gt; array(&#39;add_more_companies_submit&#39;), // If no javascript action.<br>
  &#39;#ahah&#39; =&gt; array(      <br>   &#39;path&#39; =&gt; &#39;submitresume/form/morecompanies&#39;,      <br>   &#39;wrapper&#39; =&gt; &#39;all-companies&#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; &#39;Submit&#39;,<br>);</div>
<div>return $form;<br>}</div>
<div>function submitresume_add_one_company_form($delta, $company_name, $compsdt, $compedt) {<br> $form = array(<br>  &#39;#tree&#39; =&gt; TRUE,<br> );</div>
<div> $form[&#39;companies&#39;] = array(<br>  &#39;#type&#39; =&gt; &#39;textfield&#39;,<br>  &#39;#title&#39; =&gt; t(&#39;Company&#39;).&quot;-&quot;.$delta,<br>  &#39;#parents&#39; =&gt; array(&#39;allcompanies&#39;, $delta, &#39;companies&#39;),<br>
  &#39;#size&#39; =&gt; 30,<br>  &#39;#maxlength&#39; =&gt; 100,<br> );</div>
<div> $form[&#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;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> return $form;<br>}</div>
<div><br>function add_more_companies_submit($form, &amp;$form_state) {<br> // Set the form to rebuild and run submit handlers.<br> node_form_submit_build_node($form, $form_state);</div>
<div> // Make the changes we want to the form state.<br> if ($form_state[&#39;values&#39;][&#39;morecompany&#39;]) {<br>  $n = $_GET[&#39;q&#39;] == &#39;submitresume/form/morecompanies&#39; ? 1 : 5;<br>  $form_state[&#39;all_companies&#39;] = count($form_state[&#39;values&#39;][&#39;allcompanies&#39;]) + $n; <br>
 }<br>}</div>
<div>function  submitresume_add_more_companies() {<br> include_once &#39;modules/node/node.pages.inc&#39;;<br> $form_state = array(&#39;storage&#39; =&gt; NULL, &#39;submitted&#39; =&gt; FALSE);<br> $form_build_id = $_POST[&#39;form_build_id&#39;];</div>

<div> // Get the form from the cache.<br> $form = form_get_cache($form_build_id, $form_state);<br> $args = $form[&#39;#parameters&#39;];<br> $form_id = array_shift($args);</div>
<div> // We will run some of the submit handlers so we need to disable redirecting.<br> $form[&#39;#redirect&#39;] = FALSE;</div>
<div> // 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;</div>

<div> // Build, validate and if possible, submit the form.<br> drupal_process_form($form_id, $form, $form_state);</div>
<div> // 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);</div>
<div> // Render the new output.<br> $choice_form = $form[&#39;work_history_wrapper&#39;][&#39;allcompanies&#39;];<br> unset($choice_form[&#39;#prefix&#39;], $choice_form[&#39;#suffix&#39;]); // Prevent duplicate wrappers.<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>}</div>
<div>function submitresume_validate($form, &amp;$form_state) {<br>}</div>
<div>function submitresume_submit($form, &amp;$form_state) {<br>drupal_set_message(t(&#39;The form has been submitted.&#39;));<br>}<br>?&gt;<br></div>