What are the functions in Form API that perform the task just like form load and unload I want to perform few checks when the form loads and delete those checks when the form is unloaded.

           function form_example_form($form, &$form_state) {
                 $form['search'] = array(
                '#type' => 'textfield',
                '#title' => t('Search for Members'),
                '#size' => 60,
                '#maxlength' => 128
                $form['submit'] = array(
                '#type' => 'submit',
                '#value' => t('Search'),
                '#submit' => array('myform_newform_submit'),
                 $header = array(
                    array('data' => t('ID'), 'field' => 't.id'),
                    array('data' => t('First Name'), 'field' => 't.fname'),
                    array('data' => t('Last Name'), 'field' => 't.lname'),
                    'operations' => array('data' => t('Operations'), 'colspan' => 2),
                if((variable_get('results'))!="") {

               $query = db_select('myform', 't')
              ->extend('TableSort')        //Sorting Extender
              ->fields('t', array('id', 'fname', 'lname'));
              $results = $query->execute();
              foreach($results as $node)
                    'id' => $node->id,
                    'first_name' =>$node->fname,
                    'last_name' => $node->lname,
                   'modify' => l('Modify', 'myform/modify/'.$node->id),
                    'delete' => l('Delete', 'myform/delete/' . $node->id));
               $rows = array(); 
            $form['pager_table'] = array(
              '#theme' => 'table',
              '#header' => $header,
              '#rows' => $rows,
              '#empty' => t('Empty Rows')
              // attach the pager theme
              $form['pager_pager'] = array('#theme' => 'pager');
                return $form;

                             function form_example_form_submit($form,&$form_state)

Now when I change the link how do I free the variable at form unload

  • what is the idea behind form load and unload ? make a ctrl+f and search here api.drupal.org/api/drupal/…
    – niksmac
    Commented Jun 6, 2012 at 5:24
  • I want to run a javascript function at the time of page load before the form items are displayed and when I move to some other link from this page I again want to run a function.Example:-Suppose I want to enter a session id at the time of loading the page and I want to free the session id at the time of page unload.
    – Dharmender
    Commented Jun 6, 2012 at 6:08
  • Can't you just add javascript with drupal_add_js() api.drupal.org/api/drupal/includes!common.inc/function/… If you need to add it to another modules form you can use hook_form_alter or if its your own form then add it at the start of the form function Commented Jun 6, 2012 at 7:13
  • what in the case where I want to assign a value to session at the time of loading and deleting the session at the time of unloading the form
    – Dharmender
    Commented Jun 6, 2012 at 7:19
  • In all probability you are thinking the problem wrong from drupal sense. can you elaborate on what's your usecase? Why do u wish to set a session on form load and unset on form_submit ( I hope thats what you mean to say when u say form_unload) do you wish to pass data during form submission process? Elaborate and you will get a better answer.
    – Dipen
    Commented Jun 6, 2012 at 9:07

1 Answer 1


Lets try this method

on your form

function form_example_form($form, &$form_state) {
  $name = 'myname';
  $value = 'nikhil';
  variable_set($name, $value);
  // Your form elements goes here ...   

function form_example_form_submit($form, &$form_state) {
  $name = 'myname';
  $myname = variable_get($name, $default = NULL);
  echo $myname; // will print nikhil


As i dont know any Form Unload event i will do

drupal_add_js("jQuery(document).unload(function() { 
//make an AJAX call to clear the value });", 'inline');

in javascript

Good luck.

  • 1
    what if the variable is set in submit function and retrieved in form function then how can we delete the variable when we visit other link i.e form unloads
    – Dharmender
    Commented Jun 6, 2012 at 8:36
  • If you want a more specific answer elaborate your question.
    – niksmac
    Commented Jun 6, 2012 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.