Add More Button for Text field /file field using AHAH with drupal 7 form API
Last updated on
2 August 2022
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Example Code snippet to create ADD MORE BUTTON with drupal 7 form API
function addmore_menu() {
$items = array();
$items['admin/snippet/addmore'] = array(
'title' => t('Example form'),
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('simple_form'),
'access callback' => TRUE,
);
return $items;
}
function simple_form($form, $form_state, $no_js_use = FALSE) {
$form = array();
$form['#tree'] = TRUE;
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['example_entry'] = array(
'#type' => 'fieldset',
'#title' => t('Example form'),
"#prefix" => '<div id="form-example_entry">',
"#suffix" => '</div>',
'#collapsible' => FALSE,
);
// --------------- Add more text box starts ------------------------------
$form['example_entry']['entry_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Participants Name'),
'#prefix' => '<div id="entry-fieldset-wrapper">',
'#suffix' => '</div>',
);
if (empty($form_state['pnum_names'])) {
$form_state['pnum_names'] = 1;
}
for ($i = 0; $i < $form_state['pnum_names']; $i++) {
$form['example_entry']['entry_fieldset']['users'][$i] = array(
'#type' => 'textfield',
'#title' => '',
'#size' => 22,
'#description' => t('User Name'),
'#attributes' => array('multiple' => 'multiple'),
);
}
$form['example_entry']['entry_fieldset']['add_user_name'] = array(
'#type' => 'submit',
'#value' => t('Add more user'),
'#submit' => array('users_add_more_add_one'),
'#ajax' => array(
'callback' => 'users_add_more_callback',
'wrapper' => 'entry-fieldset-wrapper',
),
);
if ($no_js_use) {
if (!empty($form['entry_fieldset']['remove_name']['#ajax'])) {
unset($form['entry_fieldset']['remove_name']['#ajax']);
}
unset($form['entry_fieldset']['add_name']['#ajax']);
}
// --------------- Add more text box ends ---------------------------------
// File Attachement form element starts
$form['example_entry']['upload_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Attachments'),
'#prefix' => '<div id="upload-fieldset-wrapper">',
'#suffix' => '</div>',
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['example_entry']['upload_fieldset']['attachement'][$i] = array(
'#type' => 'managed_file',
'#title' => '',
'#size' => 22,
'#description' => t('Upload File'),
'#attributes' => array('multiple' => 'multiple'),
);
}
$form['example_entry']['upload_fieldset']['add_name'] = array(
'#type' => 'submit',
'#value' => t('Add more'),
'#submit' => array('attachment_add_more_add_one'),
'#ajax' => array(
'callback' => 'attachment_add_more_callback',
'wrapper' => 'upload-fieldset-wrapper',
),
);
if ($no_js_use) {
if (!empty($form['upload_fieldset']['remove_name']['#ajax'])) {
unset($form['upload_fieldset']['remove_name']['#ajax']);
}
unset($form['upload_fieldset']['add_name']['#ajax']);
}
// File Attachement form element ends
$form['example_entry']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// --------------- AJAX CALLBACK FUNCTIONS FOR UPLOAD FIELD-----------------
/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
function attachment_add_more_add_one($form, &$form_state) {
if (!isset($form_state['num_names'])) {
$form_state['num_names'] = 0;
$form_state['num_names']++;
}
$form_state['num_names']++;
$form_state['rebuild'] = TRUE;
}
/**
* Callback for both ajax-enabled buttons.
*
* Selects and returns the fieldset with the names in it.
*/
function attachment_add_more_callback($form, $form_state) {
return $form['example_entry']['upload_fieldset'];
}
// --------------- AJAX CALLBACK FUNCTIONS FOR TEXT FIELD-----------------
function users_add_more_add_one($form, &$form_state) {
if (!isset($form_state['pnum_names'])) {
$form_state['pnum_names'] = 0;
$form_state['pnum_names']++;
}
$form_state['pnum_names']++;
$form_state['rebuild'] = TRUE;
}
function users_add_more_callback($form, $form_state) {
return $form['example_entry']['entry_fieldset'];
}
function simple_form_submit($form, &$form_state) {
$num_files = COUNT($form_state['values']['example_entry']['upload_fieldset']['attachement']);
if (($num_files > 0)) {
for ($i = 0; $i < $num_files; $i++) {
$attachment[$i] = file_load($form_state['values']['example_entry']['upload_fieldset']['attachement'][$i]);
}
}
echo '<pre>';
print_r($form_state);
}
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion