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

I want to create a custom form with a few textfields and then use variable_set to save those textfields as variables.

I am not able to save the variable with the code below.

Question: I do not see the drupal message on hitting submit and when I go to the 'variables' table, I do not see the variable 'id_event'. I have tried clearing all caches several times.

Code below is from *.module file of my custom module "mymodule"

    function mymodule_menu() {

     $items['example_form'] = array(
         'title'        =>  'Progress Numbers',
         'description'  =>  'Form to enter variables that will be shown on Progress Update Block on Front Page',
         'page callback'    => 'drupal_get_form',
         'page arguments'   => array('example_form_form1'),
         'access arguments' => array('access administration menu'),

     );

     return $items;
 }

The callback function:

 function example_form_form1($form, &$form_state) {
  $form['id_event'] = array(
    '#type' => 'textfield',
    '#title' => t('Id Event'),
    '#required' => TRUE,
    '#default_value' => variable_get('id_event', 7),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

Submit Function

function example_form_form1_submit(&$form, &$form_state) {
  variable_set('id_event', $form_state['values']['id_event']);
  drupal_set_message($id_event);
}
share|improve this question
1  
If you are using devel...then can you please dpm($form_state['values']['id_event']); & see whats it returning here in submit function ? –  RajeevK Mar 8 at 10:16

3 Answers

Modify your example_form_form1_submit function as follows

function example_form_form1_submit(&$form, &$form_state) {
  variable_set('id_event', $form_state['values']['id_event']);
  $id_event = variable_get('id_event', '');
  drupal_set_message($id_event);
}

enter image description here

share|improve this answer

Drupal's variable API allows for storage of persistent variables across separate requests.

If you want to set a Drupal variable, you need to use variable_set().

Example:

$my_variable = 'very_important_value';          // Here we set a php variable

variable_set('my_variable_name', $my_variable); // Here we store the php variable
                                                // into a Drupal variable

If you want to retrieve the Drupal variable's value, you need to use variable_get().

$saved_value = variable_get('my_variable_name', 'default_value'); // Get the value of the
                                                                  // Drupal variable. If
                                                                  // it does not exist, then
                                                                  // use the second argument

You are seeing the value stored correctly in the database because you are making use of variable_set() correctly. However, when you are trying to retrieve the value, you need to be sure to call variable_get() to retrieve the value and store it into a php variable for further usage.

Suggestion:

function example_form_form1_submit(&$form, &$form_state) {

  variable_set('id_event', $form_state['values']['id_event']); // Stores the value

  $saved_value = variable_get('id_event', 'Default');          // Retrieve the stored value.
                                                               // If it is not saved,
                                                               // $saved_value will be set
                                                               // to 'Default'.
  drupal_set_message($saved_value);
}
share|improve this answer

I resolved my problem using "system_settings_form($form);" since the solutions above did not work for me. (Will try later and if it works, will post any learnings here).

Following is what worked for me for reference:

My .module file:

function neil_menu() {

$items['admin/settings/neil'] = array(
    'title' => 'Node images',
    'description' => 'Control how to upload node images.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('neil_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'neil.admin.inc',
);
 return $items;

}

neil.admin.inc file:

function neil_admin_settings() {
   $form['progress_so_far'] = array(
'#type' => 'textfield',
'#title' => t('Progress So Far'),
'#default_value' => variable_get('progress_so_far', '0'),
'#maxlength' => 255,
);

return system_settings_form($form);
}
share|improve this answer

Your Answer

 
discard

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

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