0

I have prepared a form using Drupal form API. Now how can I insert the form values when I click the submit button by using db_insert()?

$form['totp'] = array(
    '#title' => t('Title of the Programme'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#maxlength' => 250, 
    '#size' => 50,    
);

$form['pd'] = array(
    '#type' => 'radios',
    '#title' => t('Programme Day'),
    '#required' => TRUE,
    '#options' => array(
        0 => 'One Day Programme',
        1 => 'Two or More Day Programmes'
    ),
);

The above values to be stored in MySQL table?

3

You would need to add a custom form submit handler.

$form['#submit'][] = 'mymodule_custom_form_submit';

Then, in the function, use $form_state['values'];

function mymodule_custom_form_submit($form, $form_state){
    db_insert('yourtable')
        ->fields(array(
            'totp' => $form_state['values']['totp'], 
            'pd' => $form_state['values']['pd']
          ))
        ->execute();    
}
1
  • 2
    +1. If your form function is "my_custom_form", "my_custom_form_submit" will be called automatically so no need to add submit handler manually. – AKS Jan 31 '13 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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