[support] How to add fields dynamically in a page

Austin Einter austin.einter at gmail.com
Thu Mar 10 13:41:29 UTC 2011


Finally my module code looks as below. I am trying to make use of AHAH ("Add
More Companies..." button), to add next row containing company name, start
date and end date fields. On clicking "Add More Companies..." button, I am
getting a dialog and says *Error occured*. Please suggest me, where I am
going wrong.


<?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_my_form_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;
 }

 $form['name'] = array(
 '#type' => 'fieldset',
 '#title' => t('Personal Information'),
 '#collapsible' => TRUE,
 '#collapsed' => FALSE,
 );
 $form['name']['persname'] = array(
 '#type' => 'textfield',
 '#title' => t('Name'),
 '#required' => TRUE,
 '#description' => "Please enter your name.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['name']['email'] = array(
 '#type' => 'textfield',
 '#title' => t('Email ID'),
 '#required' => TRUE,
 '#description' => "Please enter your valid email-id.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['name']['mobile'] = array(
 '#type' => 'textfield',
 '#title' => t('Mobile Number'),
 '#required' => TRUE,
 '#description' => "Please enter your valid mobile number.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['name']['birthdate'] = array(
 '#type' => 'textfield',
 '#title' => t('Date of Birth'),
 '#required' => TRUE,
 '#description' => "Please enter your date of birth in DD/MM/YYYY format.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['work'] = array(
 '#type' => 'fieldset',
 '#title' => t('Work Information'),
 '#collapsible' => TRUE,
 '#collapsed' => FALSE,
 );
 $form['work']['yearsexp'] = array(
 '#type' => 'textfield',
 '#title' => t('Years of Experience'),
 '#description' => "Please enter your years of experience.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['work']['dlocation'] = array(
 '#type' => 'textfield',
 '#title' => t('Preferred work location'),
 '#description' => "Please enter your preferred work location.",
 '#size' => 20,
 '#maxlength' => 20,
 );
 $form['resume'] = array(
 '#type' => 'file',
 '#title' => "Upload Resume",
 '#description' => 'Browse and select your resume',
 '#required' => TRUE,
 );

////////////////////////////////////////////////////////////////
for ($delta = 0; $delta < $all_companies; $delta++)
{
 //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>',
 );
 $form['work_history_wrapper']['allcompanies']['company'] = array(
 '#type' => 'textfield',
 '#title' => t('Company'),
 '#size' => 30,
 '#maxlength' => 100,
 );
 $form['work_history_wrapper']['allcompanies']['sdate'] = array(
 '#type' => 'date',
 '#title' => t('Start Date'),
 );
 $form['work_history_wrapper']['allcompanies']['edate'] = array(
 '#type' => 'date',
 '#title' => t('End Date'),
 );
}
 $form['work_history_wrapper']['morecompany'] = array(
'#type' => 'submit',
'#value' => t('Add More Companies...'),
'#weight' => 1,
//'#submit' => array('add_more_companies_submit'), // If no javascript
action.
'#ahah' => array(
'path' => 'submitresume/form/morecompanies',
'wrapper' => 'work-history-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);

///////////////////////////////////////////////////////////////
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
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_my_form_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) {
}
// Adds a submit handler/function to our form to send a successful
// completion message to the screen.

function submitresume_submit($form, &$form_state) {
drupal_set_message(t('The form has been submitted.'));
}
?>




























On Thu, Mar 10, 2011 at 8:08 AM, Austin Einter <austin.einter at gmail.com>wrote:

> Hi
> I am making a custom form, where I want to have work history of a person in
> one line and at the end of line a button "add more".
> It will look some thing like as below -
>
> *Company  *       *Start-Date*      *End-Date*       *Add More ...*
> **
> If user clicks on button "*Add More ...*" then it should add one more line
> and my form should look as below
>
>  *Company  *       *Start-Date*      *End-Date*       *Add More ...*
>  *Company  *       *Start-Date*      *End-Date*       *Add More ...*
>
> So how many times user clicks "*Add More ...*", those many rows should be
> shown up.
>
> How can I acheive it  in Drupal. As per documentation and google search
> AHAH is capable of doing it.
> Please correct me if wrong.
>
> My custom form code is as below. My questions are -
>
> 1. How can I add 4 fields in one line. By default these are coming  below
> of earlier field.
> 2. How to make use of AHAH.
> 3. Also  one more challenge for me is how do act on "onClick" of  "*Add
> More ...*" button, by default it may call submit handler.
>
> A step by step approach will help me, as first time I am looking at AHAH.
>
>  Best Regards
> Austin
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20110310/d3b56aa3/attachment-0001.html 


More information about the support mailing list