drupal-7drupal-forms

form submission in Drupal


i have made a custom module for form in drupal using dupal API,and made a table in my database What i want is to save the value of drop down box instead of it's index into database...my .module file is:

<?php
function form_test_menu() {
$items['formtest'] = array(
    'title' => 'Form Test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_form'),
    'access callback' => TRUE,
);
return $items;

}

function form_test_form($form,&$form_submit) {
$form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
);
$form['college'] = array(
    '#title' => t('College/University'),
    '#type' => 'textfield',
    '#required' => TRUE,
);
$form['education'] = array(
    '#title' => t('Education'),
    '#type' => 'select',
    '#description' => 'Select your higher education .',
    '#options' => array(t('--- SELECT ---'), t('B.tech'), t('MCA'), t('MBA'),t('Graduate')),
    '#required' => TRUE,
);
$form['percentage'] = array(
    '#title' => t('Percentage/CGPA'),
    '#type' => 'textfield',
    '#required' => TRUE,
);
$form['application'] = array(
    '#title' => t('Job Applied'),
    '#type' => 'select',
    '#options' => array(t('---select---'),t('Web Developer'),t('Web Designer'),t('SEO'),t('Marketing')),
    '#required' => TRUE,
);


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

}

function form_test_form_submit($form, $form_state) {
global $user;
// Here u can insert Your custom form values into your custom table.
db_insert('drupal')
    ->fields(array(
        'name' => $form_state['values']['name'],
        'college' => $form_state['values']['college'],
        'education' => $form_state['values']['education'],
        'percentage' => $form_state['values']['percentage'],
        'application' => $form_state['values']['application'],

    ))->execute();
drupal_set_message("successfully saved data");

} ?>

All are in one file(.module)....any help will be appreciated. Thank you.


Solution

  • The solution is to pass options array from one function to another. To achieve that use the second argument of "form_test_form" and "form_test_form_submit" functions.

    function form_test_form($form,&$form_submit) {
      $form_submit['myoptions'] = array(' select', 'option1' .....
    }
    

    Then

    function form_test_form_submit($form,&$form_state) {    
     $myoptions = $form_state['myoptions']; etc ...
    }