I have created a form which contains two text fields and a submit button. My goal is to save those data to database get it back when needed. I'm using variable_set() and variable_set() functions to store and retrieve data. I don't know whether it is correct or not. Please guide me. Here is my code :

/*
* Implements hook_menu().
*/
function oseds_m_add_media_site_menu() {
    $items['admin/config/system/profile'] = array(
    'title' => 'Profile',
    'description' => 'Tell Drupal where to get photos and videos path.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('add_media_site_form'),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM
    );
  return $items;
}
/*
* Define a form.
*/
function add_media_site_form($form) {
$form = array();
  $form['photos'] = array(
                        '#title' => t('Photo Url'),
                        '#type' => 'textfield',
                        '#description' => t('Get photos url path.'),
                        '#default_value' => variable_get('photos'),
                        );
$form['videos'] = array(
                      '#title' => t('Video Url'),
                      '#type' => 'textfield',
                      '#description' => t('Get videos url path.'),
                      '#default_value' => variable_get('videos'),
                      );
  $form['submit'] = array(
                        '#type' => 'submit',
                        '#value' => t('Save')
                           );
    return $form;
}
/*
* Handle post-validation form submission.
*/
function add_media_site_form_submit($form, &$form_state) {
//$photos = $form_state['input']['photos'];
//$videos = $form_state['input']['videos'];
if (intval($form_state['values']['photos']) != NULL) {
    $ph = variable_get('photos_url_fid', intval($form_state['values']['photos']));
    $vi = variable_get('videos_url_fid', intval($form_state['values']['videos']));
   }
    drupal_set_message(t('Your configuration has been saved.'));
}

And I want to know where the given variables are stored on database.
Thanks in advance.

Comments

Hi,

My goal is to save those data to database get it back when needed.

If you want to save both textbox values in DB without any amendment, then you can directly return the system_settings_form() instead of $form.

I have updated your code with this function.

And I want to know where the given variables are stored on database.

"variable" table stores these two values with repsective name i.e photos and videos

<?php
/*
* Implements hook_menu().
*/
function oseds_m_add_media_site_menu() {
   
$items['admin/config/system/profile'] = array(
   
'title' => 'Profile',
   
'description' => 'Tell Drupal where to get photos and videos path.',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('add_media_site_form'),
   
'access callback' => TRUE,
   
'type' => MENU_NORMAL_ITEM
   
);
  return
$items;
}
/*
* Define a form.
*/
function add_media_site_form($form, &$form_state) {
$form = array();
 
$form['photos'] = array(
                           
'#title' => t('Photo Url'),
                           
'#type' => 'textfield',
                           
'#description' => t('Get photos url path.'),
                           
'#default_value' => variable_get('photos'),
                            );
   
$form['videos'] = array(
                             
'#title' => t('Video Url'),
                             
'#type' => 'textfield',
                             
'#description' => t('Get videos url path.'),
                             
'#default_value' => variable_get('videos'),
                              );
 
$form['submit'] = array(
                           
'#type' => 'submit',
                           
'#value' => t('Save')
                           );
  return
system_settings_form($form);
}
?>

If you want to do any amendment before saving in DB then you can use hook_submit() function as you have used but please check variable table structure in DB once to get better understanding of this. There is two column in this table, one is 'name' with datatype varchar and other is 'value' with datatype longblob.

So my opinion is first approach is correct, because if you do any change in post values before insertion/updation in DB then you also need to change logic for '#default_value' attribute where you are directly using variable_get().

<?php
/*
* Implements hook_menu().
*/
function oseds_m_add_media_site_menu() {
   
$items['admin/config/system/profile'] = array(
   
'title' => 'Profile',
   
'description' => 'Tell Drupal where to get photos and videos path.',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('add_media_site_form'),
   
'access callback' => TRUE,
   
'type' => MENU_NORMAL_ITEM
   
);
  return
$items;
}
/*
* Define a form.
*/
function add_media_site_form($form, &$form_state) {
$form = array();
 
$form['photos'] = array(
                           
'#title' => t('Photo Url'),
                           
'#type' => 'textfield',
                           
'#description' => t('Get photos url path.'),
                           
'#default_value' => variable_get('photos'),
                            );
   
$form['videos'] = array(
                             
'#title' => t('Video Url'),
                             
'#type' => 'textfield',
                             
'#description' => t('Get videos url path.'),
                             
'#default_value' => variable_get('videos'),
                              );
 
$form['submit'] = array(
                           
'#type' => 'submit',
                           
'#value' => t('Save')
                           );
    return
$form;
}
/*
* Handle post-validation form submission.
*/
function add_media_site_form_submit($form, &$form_state) {
   
//$photos = $form_state['input']['photos'];
    //$videos = $form_state['input']['videos'];
    
if (intval($form_state['values']['photos']) != NULL) {
   
$ph = variable_get('photos_url_fid', intval($form_state['values']['photos']));
   
$vi = variable_get('videos_url_fid', intval($form_state['values']['videos']));
   }
   
drupal_set_message(t('Your configuration has been saved.'));
}
?>

Note : Please always put PHP code inside <php ?> tags.

Thanks

Pushpinder Rana

Another nice example of using system_settings_form() can be seen here: https://drupal.org/node/1111212 (you will need to go on to page 2).

Note that this is a subsection of the tutorial on creating D7 modules: https://drupal.org/node/1074360

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thanks to all helping me. I finally got the result. I used the following code:

<?php
function add_media_site_form_submit($form, &$form_state) {
 
$photos = variable_set('photos_url_fid', ($form_state['values']['photos_url_fid']));
 
$videos = variable_set('videos_url_fid', ($form_state['values']['videos_url_fid']));
 
drupal_set_message(t('The configuration options have been saved'));
}
?>

I used variable_get() instead of variable_set() function. That's why I didn't get the result as I expected. Now it's working. Many thanks for your support.

Good