Skip to content

Instantly share code, notes, and snippets.

@rakeshjames
Created October 1, 2014 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rakeshjames/65d7f54a5e7574ab1dec to your computer and use it in GitHub Desktop.
Save rakeshjames/65d7f54a5e7574ab1dec to your computer and use it in GitHub Desktop.
Drupal 7 Ajax call back on form submit example
<?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'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment