23 October 2011

How to create forms that are submitted via ajax. To understand this tutorial you have to have a basic knowledge of Drupal programming and its form api.

Simple form which has name field and submit button. In result it prints the name you inserted and every time you submit the form again it changes.
Source code viewer
  1. function HOOK_form($form, &$form_state) {
  2.  
  3. $form['name'] = array(
  4. '#title' => 'Name',
  5. '#type' => 'textfield',
  6. '#description' => 'Your name.',
  7. '#prefix' => '<div id="RESULT_DIV_ID">',
  8. '#suffix' => '</div>',
  9. );
  10.  
  11. $form['submit'] = array(
  12. '#type' => 'submit',
  13. '#value' => 'Submit',
  14. '#ajax' => array(
  15. 'callback' => 'CALLBACK_FUNCTION_NAME',
  16. 'wrapper' => 'RESULT_DIV_ID',
  17. ),
  18. );
  19. return $form;
  20. }
  21.  
  22. function CALLBACK_FUNCTION_NAME($form, &$form_state) {
  23. return '<div id="RESULT_DIV_ID">Your name is :'.$form_state['values']['name'].'</div>';
  24. }
Programming Language: PHP
All ajax options: