0

I have used variable_get(), and variable_set() in my form search, but when I visit another page, and come to the module again, it shows the previous results. How can I free the variable at the time of reloading the module?

/**
* @file
* The theme system, which controls the output of Drupal.
*/
function myform_help($path, $arg) {
    switch ($path) {
        case "admin/help#myform":
        return '<p>' . t("MyForm Module") . '</p>';
        break;
    }
}

function myform_menu() {
    $items['myform/newform'] = array(
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('myform_newform'),
        'access callback' =>TRUE
    );
    return $items; 
}

function myform_newform($form, &$form_state) {
    //Create a list of headers for your Html table (see Drupal 7 docs for theme_table here

    $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(
        // The header gives the table the information it needs in order to make
        // the query calls for ordering. 
        array('data' => t('ID'), 'field' => 't.id'),
        array('data' => t('First Name'), 'field' => 't.fname'),
        array('data' => t('Last Name'), 'field' => 't.lname'),
    );

    if((variable_get('results'))!="") {
        $query = db_select('myform', 't')
            ->extend('TableSort')        //Sorting Extender
            ->orderByHeader($header)
            ->extend('PagerDefault')
            ->condition('lname',variable_get('results'),'=')
            ->limit(2)
            ->fields('t', array('id', 'fname', 'lname'));
        $results = $query->execute();
        foreach($results as $node)
        {
            $rows[$node->id]=array('data'=>(array)$node);
        }
    }
    else {
        $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 myform_newform_submit($form,&$form_state) {
    variable_set('results',$form_state['values']['search']);
}
6
  • 1
    variable_set() or variable_get() will store information in the variable table so I think it is not the better way. You can use sessions or cookies for this kind of functionality. Jun 5, 2012 at 5:26
  • Yup Sessions and cookies could be used but I want to know is there any other way using variable set and get methods
    – Dharmender
    Jun 5, 2012 at 5:32
  • 3
    You can do by using variable_del($name) function but I think it not the better way. By this variable_del($name) function you can delete the stored value from the variable table. Jun 5, 2012 at 5:33
  • Where to add variable_del($name) so that when form gets unloaded it should delete the variable which have been set
    – Dharmender
    Jun 5, 2012 at 5:47
  • where you are creating your form array Jun 5, 2012 at 5:49

1 Answer 1

0

In this specific case, you don't need to use persistent variables. It's sufficient to set $form_state['rebuild'] to TRUE in the form submission handler and set a value in the $form_state array that is then checked by the form builder function. The workflow in these cases is the following.

  • The form is used to ask input from users
  • The form submission handler uses that input to query the database and stores the result in $form_state (for example, in $form_state['rows'])
  • The form submission handler sets $form_state['rebuild'] to TRUE
  • The form builder checks the value in $form_state is set (for example, with if (isset($form_state['rows'])) { /* ... */ }) and shows those values in a table before the form

The array key used to store the value just needs to be a key that doesn't conflict with Drupal core or contributed modules core. To avoid that, the key name is usually prefixed by the module machine name (or it's the module machine name).

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.