Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By madhusudan on
Hi,
I want to apply ajax on buttons, I used "textfield" to enter number of checkboxes, so that user can enter any number in the textfield.
I want to use the value of the textfield to regenerate the checkboxes in the same form, hence refreshing the whole form.
but nothing happens when I click on "showbox" button.
please point out any wrong in the code.. or is there any example which implements this ?.
code:
function howmany_form($form, &$form_state) {
$form['howmany']['text'] = array(
'#title' => t('Enter text'),
'#type' => 'textfield',
'#size' => '1',
'#tree' => TRUE,
);
$form ['button'] = array(
'#type' => 'button',
'#value' => t('showbox'),
'#ajax' => array(
'event' => 'click',
'callback' => 'ajax_autocheckboxes_callback',
'wrapper' => 'checkboxes-div',
'method' => 'replace',
),
);
$form['howmany_select'] = array(
'#type' => 'fieldset',
'#title' => t("Generated Checkboxes"),
'#prefix' => '<div id="checkboxes-div">',
'#suffix' => '</div>',
'#description' => t('This is where we get automatically generated checkboxes'),
);
$num_checkboxes = !empty($form_state['values']['howmany']['text']) ? $form_state['values']['howmany']['text'] : 1;
for ($i=1; $i<=$num_checkboxes; $i++) {
$form['howmany_select']['checkboxes_fieldset']["checkbox$i"] = array(
'#type' => 'checkbox',
'#title' => "Checkbox $i",
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
//ajax form call back function
function ajax_autocheckboxes_callback($form, $form_state) {
return $form['howmany_select'];
}
Comments
Check out Examples module. It
Check out Examples module. It has an example of how to generate check boxes using ajax.
---
Drupalika | دروپال فارسی
I tried those examples..
@sebto,
Thanks for replying.. I have seen those examples... but in my case its little advanced.. and its not working..
Change is the only Constant
Try
Try changing
to
there is no point in changing that
@maartenstorm
I have set '#tree' => TRUE, there is no point in changing that. but strange things happening.
I am not getting the value in callback function if '#tree' => TRUE, if i don't specify '#tree' => TRUE, and if I use $form_state['values']['text'] in callback function I will get the value whatever user entered.
any other suggestions..?
Change is the only Constant
I overlooked you were using a
I overlooked you were using a tree.
Setting '#tree' => TRUE in $form['howmany'] will give you the $form_state['values']['howmany']['text'] you need.
anyone tried this code..?
anyone tried this code..?
how to debug and make this work..?
Change is the only Constant
I did try your code and I did
I did try your code and I did test my solution. What happens when you set the #tree attribute not in $form['howmany']['text'] but in its parent $form['howmany']?
Same nothing happens!
Same nothing happens!, I modified the code, and made all fields into one form.
Change is the only Constant
Did you mean
Did you mean $form_state['values']['text'] instead of $form_state['values']['entered_text']?
oh yes.. its
oh yes.. its $form_state['values']['text'] . sorry for the typo....
Change is the only Constant
Hi, I took you code any tried
Hi,
I took you code any tried in my local on D7. I have made some correction in your code. It is working fine.
Please use the following code.
cool.. thanks man..
Thanks hemanth, its working.. strange, my first posted code and yours are almost similar. you just changed names.. and it worked..!?
the '&' in callback is not needed it works with or without that..
Change is the only Constant
another solution
For me it was not working. The button press always ended in a submit of the form reloading the page.
But I could solve it by firing a change event via jQuery on a checkbox. For the code I use a #markup form element:
AJAX callback on button click
Posting this here to help future Googlers:
You can have Drupal fire a AJAX callback on a button click by mapping '#type' to 'submit ajax-trigger' and 'submitter' to FALSE (in the '#ajax' array). If the callback still doesn't load, try calling unset on the element in $form_state['values']. The code below worked for me: