<div>Hi All</div>
<div>I have a small working custom module. Code is as below.</div>
<div> </div>
<div>It'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 "add/remove more companies" button.</div>
<div> </div>
<div>If user clicks "add/remove more companies" 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><?php</div>
<div>function resume_submit_menu() {<br> $items = array();<br> $items['resume_submit/form'] = array(<br> 'title' => t('Submit Your Resume'),<br> 'page callback' => 'resume_submit_form',<br>
'access arguments' => array('access content'),<br> 'type' => MENU_CALLBACK,<br> );</div>
<div> $items['resume_submit/form/morecompanies'] = array(<br> 'page callback' => 'resume_submit_add_more_companies',<br> 'access arguments' => array('access content'),<br> 'type' => MENU_CALLBACK,<br>
);</div>
<div> return $items;<br>}</div>
<div>function resume_submit_form() {<br> return drupal_get_form('resume_submit_my_form');<br>}</div>
<div>function resume_submit_my_form($form_state) {<br> $form = array('#cache' => TRUE,);<br> <br> if($form_state['values']['numOfCompanies'] == '')<br> {<br> $all_companies = 1;<br> }<br>
else<br> {<br> $all_companies = $form_state['values']['numOfCompanies'];<br> }</div>
<div> $form['numOfCompanies'] = array(<br> '#type' => 'value',<br> '#value' => t($all_companies),<br> );<br> <br> //Add a wrapper for the companies and add more button.<br> $form['work_history_wrapper'] = array(<br>
'#title' => t("Work history: Mention all the companies you gave worked so far"),<br> '#type' => 'fieldset',<br> '#collapsible' => TRUE,<br> '#collapsed' => FALSE,<br>
'#prefix' => '<div class="clear-block" id="work-history-wrapper">',<br> '#suffix' => '</div>',<br> );</div>
<div> //Container for just companies<br> $form['work_history_wrapper']['allcompanies'] = array(<br> '#prefix' => '<div id="all-companies">',<br> '#suffix' => '</div>',<br>
'#tree' => 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 < $all_companies; $delta++)<br> {<br> $company_name = isset($form_state['values']['allcompanies'][$delta]['companies']) ? $form_state['values']['allcompanies'][$delta]['companies'] : '';</div>
<div> $form['work_history_wrapper']['allcompanies'][$delta]['companies'] = array(<br> '#type' => 'textfield',<br> '#title' => t('Company'.'/'.($delta + 1)),<br>
'#parents' => array('allcompanies', $delta, 'companies'),<br> '#value' => t($company_name),<br> '#size' => 30,<br> '#maxlength' => 50,<br> );</div>
<div> $form['work_history_wrapper']['allcompanies'][$delta]['startdate'] = array(<br> '#type' => 'date',<br> '#title' => t('Start Date'),<br> '#parents' => array('allcompanies', $delta, 'startdate'),<br>
);</div>
<div> $form['work_history_wrapper']['allcompanies'][$delta]['enddate'] = array(<br> '#type' => 'date',<br> '#title' => t('End Date'),<br> '#parents' => array('allcompanies', $delta, 'enddate'),<br>
);</div>
<div> $form['work_history_wrapper']['allcompanies'][$delta]['removecompany'] = array(<br> '#type' => 'checkbox',<br> '#title' => t('Check to remove'),<br>
'#parents' => array('allcompanies', $delta, 'removecompany'),<br> ); <br> }<br> <br> $form['work_history_wrapper']['morecompany'] = array(<br> '#type' => 'submit',<br>
'#value' => t('Add / Remove Companies'),<br> '#weight' => 1,<br> '#ahah' => array( <br> 'path' => 'resume_submit/form/morecompanies', <br> 'wrapper' => 'all-companies', <br>
'method' => 'replace', <br> ),<br> );</div>
<div>$form['submit'] = array(<br>'#type' => 'submit',<br>'#value' => 'Submit',<br>'#submit' => array('resume_submit_submit'),<br>);</div>
<div>return $form;<br>}</div>
<div>function resume_submit_add_more_companies() {</div>
<div> include_once 'modules/node/node.pages.inc';<br> $form_state = array('storage' => NULL, 'submitted' => FALSE);</div>
<div> $form_build_id = $_POST['form_build_id'];<br> <br> // Get the form from the cache.<br> $form = form_get_cache($form_build_id, $form_state);<br> <br> $args = $form['#parameters'];<br> $form_id = array_shift($args);<br>
<br> // We will run some of the submit handlers so we need to disable redirecting.<br> $form['#redirect'] = FALSE;<br> <br> // We need to process the form, prepare for that by setting a few internals<br> // variables.<br>
$form['#post'] = $_POST;<br> $form['#programmed'] = FALSE;<br> $form_state['post'] = $_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("3. Output is: ").t($form_state['values']['numOfCompanies']));<br> $form_state['values']['numOfCompanies'] = $form_state['values']['numOfCompanies'] + 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['work_history_wrapper']['allcompanies'];<br> <br> unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers.<br> <br> $output = theme('status_messages') . drupal_render($choice_form);<br>
drupal_json(array('status' => TRUE, 'data' => $output));<br> <br>}</div>
<div>function resume_submit_validate($form, &$form_state) {<br>}<br> <br>function resume_submit_submit($form, &$form_state) {<br>}</div>
<div>?><br></div>