Ajax

7 replies [Last post]
shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13

Hi.

Saw the video "Using the #ajax attribute for dynamic form building" and tried to do an "Add one" button.

For some reason each time I press the "Add" button a few fields are added, including another set of 'add' and 'submit' buttons

Debugger shows the values that I expect, but I must be getting something wrong.

Any ideas?

Code is also pasted (with code coloring) @ http://www.phpriot.com/88875

<?php
function input_menu() {
variable_set('c', 1);
$items['input/examples/more'] = array(
'title' => 'More elements and attributes',
'description' => 'More complex example with additional form elements and attributes.',
'page callback' => 'drupal_get_form',
'page arguments' => array('input_more_form'),
'access callback' => TRUE,
'weight' => 1,
);
return $items;
}

function input_more_form($form, &$form_state) {
$form['first_name'][0] = array(
'#title' => t('first name'),
'#type' => 'textfield',
'#required' => TRUE,
);

$form['last_name'][0] = array(
'#title' => t('last name'),
'#type' => 'textfield',
'#required' => TRUE,
);

// Setting an empty element with a wrapper to be populated.
$form['addithere'] = array(
'#type' => 'markup',
'#prefix' => '

',
'#suffix' => '

',
);

// Added AJAX callback.
$form['add_button'] = array(
'#type' => 'button',
'#value' => 'add one yo',
'#ajax' => array(
'callback' => 'input_os_verify_ajax_callback',
'wrapper' => 'div_wrapper_put_it_here'),
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);

return $form;
}

/**
* AJAX callback for OS verification.
*/
function input_os_verify_ajax_callback($form, $form_state) {
$c = variable_get('c');

$form['last_name'][$c] = array(
'#title' => t('last name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => 'fichs'
);

$form['first_name'][$c] = array(
'#title' => t('first name'),
'#type' => 'textfield',
'#required' => TRUE,
);

$c+=1;
variable_set('c', $c);
//}
return $form;
}

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
I put values and they are

I put values and they are shown more than once. Is this function not supposed to be called only once per page?

function input_more_form($form, &$form_state) {
$form['first_name'][0] = array(
'#title' => t('first name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => "1a",
);

$form['last_name'][0] = array(
'#title' => t('last name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => "1b",
);

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
ok... after a lot of time,

ok... after a lot of time, finally figured out that even if function

input_os_verify_ajax_callback

only returns $forms, the ajax wrapper is containing a full copy of the form so everything is appearing twice. I must be doing something very stupid here...

<?php
function input_os_verify_ajax_callback($form, $form_state) {
return $form;
}

/**
* Implements hook_menu().
*/
function input_menu() {
variable_set('c', 1);
$items['input/examples/more'] = array(
'title' => 'More elements and attributes',
'description' => 'More complex example with additional form elements and attributes.',
'page callback' => 'drupal_get_form',
'page arguments' => array('input_more_form'),
'access callback' => TRUE,
'weight' => 1,
);

return $items;
}

function input_more_form($form, &$form_state) {
$form['first_name'][0] = array(
'#title' => t('first name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => "1a",
);

$form['last_name'][0] = array(
'#title' => t('last name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => "1b",
);

// Setting an empty element with a wrapper to be populated.
$form['addithere'] = array(
'#type' => 'markup',
'#prefix' => '

',
'#suffix' => '

',
);

// Added AJAX callback.
$form['add_button'] = array(
'#type' => 'button',
'#value' => 'add one yo',
'#ajax' => array(
'callback' => 'input_os_verify_ajax_callback',
'wrapper' => 'div_wrapper_put_it_here'),
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);

return $form;
}

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
Almost there! Found out the

Almost there!

Found out the problem and now there is just a very small one left.
In the following code, how can I return the commented values as well?
I need to add 2 fields, not just one.

Thanks.

function input_os_verify_ajax_callback($form, $form_state) {
$c = variable_get('c');

$form['last_name'][$c] = array(
'#title' => t('last name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => 'adding1a' . $c,
);

/*
$form['first_name'][$c] = array(
'#title' => t('first name'),
'#type' => 'textfield',
'#required' => TRUE,
'#value' => 'adding1b' .$c,
);
*/
$i=$c;
$c+=1;
variable_set('c', $c);

return $form['last_name'][$i];

}

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
Thread resolved!

Thread resolved!

Chris Shattuck
Offline
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Awesome, great work, Shay!

Awesome, great work, Shay!

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
Thanks Chris. This however

Thanks Chris. This however led to another problem - following function returns a value, but on multiple calls to the function (each time a button is pressed), only the last value is displayed on the screen, so I guess the value it returns is not actually being added to the $form.

function input_os_verify_ajax_callback($form, $form_state) {
...
return $form['family'][$i];
}

Any suggestions?

shay
Offline
Last seen: 10 years 12 weeks ago
Joined: 2012-02-13
(i reckon i can do a

(i reckon i can do a variable_set/get for all the newly created values and do a loop to add them to the $form every time the function is called, but there has to be a better solution, no?)

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.