[development] AHAH Questions
Austin Einter
austin.einter at gmail.com
Fri Mar 11 03:10:14 UTC 2011
Hi All
I am quite new to AHAH, strugling to find how it works. I referred
documentation and poll module.
I want 3 fields in a fieldset, *Company, Start-Date, End-Date*.
I have a button "*Add More Company*", if clicked, it should add a new row
having *Company, Start-Date, End-Date *dynamically.
How many times user clicks, those many rows needs to be added.
To get the above mentioned form, I have put the code as below.
However, on button click, new rows are not adding up.
Please help me to find whats the error with code.
Regards,
Austin
Code
===============================================
<?php
function submitresume_menu() {
$items = array();
$items['submitresume/form'] = array(
'title' => t('Submit Your Resume'),
'page callback' => 'submitresume_form',
'access arguments' => array('access content'),
'description' => t('Submit Your Resume'),
'type' => MENU_CALLBACK,
);
$items['submitresume/form/morecompanies'] = array(
'page callback' => 'submitresume_add_more_companies',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function submitresume_form() {
return drupal_get_form('submitresume_my_form');
}
function submitresume_my_form($form_state) {
$form = array('#cache' => TRUE,);
if (isset($form_state['all_companies'])) {
$all_companies = $form_state['all_companies'];
}
else {
$all_companies = 1;
}
//Add a wrapper for the companies and add more button.
$form['work_history_wrapper'] = array(
'#title' => t("Work history: Mention all the companies you gave worked so
far"),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#prefix' => '<div class="clear-block" id="work-history-wrapper">',
'#suffix' => '</div>',
);
//Container for just companies
$form['work_history_wrapper']['allcompanies'] = array(
'#prefix' => '<div id="all-companies">',
'#suffix' => '</div>',
);
for ($delta = 0; $delta < $all_companies; $delta++)
{
//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
$form['work_history_wrapper']['allcompanies'][$delta] =
submitresume_add_one_company_form($delta, $company_name, $compsdt,
$compedt);
}
$form['work_history_wrapper']['morecompany'] = array(
'#type' => 'button',
'#value' => t('Add More Companies'),
'#weight' => 1,
'#submit' => array('add_more_companies_submit'), // If no javascript
action.
'#ahah' => array(
'path' => 'submitresume/form/morecompanies',
'wrapper' => 'all-companies',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function submitresume_add_one_company_form($delta, $company_name, $compsdt,
$compedt) {
$form = array(
'#tree' => TRUE,
);
$form['companies'] = array(
'#type' => 'textfield',
'#title' => t('Company')."-".$delta,
'#parents' => array('allcompanies', $delta, 'companies'),
'#size' => 30,
'#maxlength' => 100,
);
$form['startdate'] = array(
'#type' => 'date',
'#title' => t('Start Date'),
'#parents' => array('allcompanies', $delta, 'startdate'),
);
$form['enddate'] = array(
'#type' => 'date',
'#title' => t('End Date'),
'#parents' => array('allcompanies', $delta, 'enddate'),
);
return $form;
}
function add_more_companies_submit($form, &$form_state) {
// Set the form to rebuild and run submit handlers.
node_form_submit_build_node($form, $form_state);
// Make the changes we want to the form state.
if ($form_state['values']['morecompany']) {
$n = $_GET['q'] == 'submitresume/form/morecompanies' ? 1 : 5;
$form_state['all_companies'] =
count($form_state['values']['allcompanies']) + $n;
}
}
function submitresume_add_more_companies() {
include_once 'modules/node/node.pages.inc';
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable
redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
$choice_form = $form['work_history_wrapper']['allcompanies'];
unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent
duplicate wrappers.
$output = theme('status_messages') . drupal_render($choice_form);
drupal_json(array('status' => TRUE, 'data' => $output));
}
function submitresume_validate($form, &$form_state) {
}
function submitresume_submit($form, &$form_state) {
drupal_set_message(t('The form has been submitted.'));
}
?>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110311/fabbfef1/attachment.html
More information about the development
mailing list