Creating Ajax Form in Drupal Module
Ajax
forms offers dynamic behavior that has no reloads of the page which can be
easily created and manipulated. They are simplified in drupal 7.
The
background request will be submitted to the server by the #ajax property
when clicked.The form will be rebuilt on the server by the function
form-builder, and then the callback function #ajax['callback'] is
called, which selects the form's portion to return for replacement on the
original page.
To
create form with Ajax-enabled,
- Use the #ajax property
to mark Ajax-enabled form element.
- A callback function has to
be called.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| <?php
function ajax _autocheckboxes($form, &$form_state)
{
$default = !empty($forms_state['values']['howmanyto_select']) ? $forms_state['values']['howmanyto_select'] : $form['howmanyto_select'] = array
(
'#title' => t(checkboxes that you want?'),
'#type' => 'select',
'#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
'#default_value' => $default,
'#ajax' => array
(
'callback' => 'ajax _callback',
'wrapper' => 'checkboxes-div',
'method' => 'replace',
'effect' => 'fade'
),
);
$form['checkboxes_fieldset'] = array
(
'#title' => t("Generated Checkboxes"),
'#prefix' => '<div id="checkboxes-div">',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#description' => t('This is where we get automatically generated checkboxes'),
);
$form['submit']=array
(
'#type'=>'submit',
'#value'=>t('Submit mee!')
);
return $form;
}
|
#ajax property
=> On click, it will make a Ajax call.
#ajax['callback']
=> This is simple function which selects and return the form that has to be
replaced.
$form_state['values']['howmanyto_select']
=> For the form element to affect ID of the wrapper $form['checkboxes_fieldset']
When
the 'howmanyto_select' element is changed,the server receives a
request that the form has to be rebuilt. After rebuilt of the form, the
callback is called:
<?php
function ajax _callback($form, $form_state)
{
return $form['checkboxes_fieldset'];
}
The
callback function selects form's portion which is to be returned and has to be
replaced on the html page. That form's portion is later rendered and returned
to the page, where the #ajax['wrapper'] has replaced.
ZenRays provide the following to make you expert
- Fully practical and project based sessions from first class.
- Training by Experienced Consultants, not regular Trainers
- and enthusiastic faculty to clear your doubts 24X7
- zenrays Free Live project after the training to get you industry experience
If you want more
details please visit us:
Zenrays.com Reach us at
trainings@zenrays.com
ABOUT THE AUTHOR
Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible
0 comments:
Post a Comment