Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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']);
}
share|improve this question
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. – Mohammad Anwar Jun 5 '12 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 '12 at 5:32
2  
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. – Mohammad Anwar Jun 5 '12 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 '12 at 5:47
where you are creating your form array – Mohammad Anwar Jun 5 '12 at 5:49
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.