The following code from the "example.module" will help you to find the example using AJAX form with one textfield called “Name”. When the submit button is pressed, it shows the output “Hello <Name>”.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @File | |
* The following code from the "example.module" will help you to find the example using AJAX form with one textfield called “Name”. When the submit button is pressed, it shows the output “Hello <Name>”. | |
*/ | |
/** | |
* Implementing hook_menu(). | |
*/ | |
function example_menu() { | |
$items['example/ajaxform'] = array( | |
'title' => 'My form', | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('my_form'), | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK, | |
); | |
return $items; | |
} | |
/** | |
* call back function. | |
*/ | |
function my_form() { | |
$form['name'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Name'), | |
'#prefix' => '<div id="my-name">', | |
'#required' => TRUE, | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Submit'), | |
'#ajax' => array( | |
'callback' => 'hello_me', | |
'wrapper' => 'my-name', | |
'method' => 'replace', | |
'effect' => 'fade', | |
), | |
'#suffix' => '</div>', | |
); | |
return $form; | |
} | |
/** | |
* ajax Call back function. | |
*/ | |
function hello_me($form, $form_state) { | |
return $form['name']='hello ' . $form_state['values']['name']; | |
} |
Comments
Post a Comment