I am working with a simple form and want to make a submit function, but I don't know how to make sure that my function will be called when I push the submit button I have defined for my form.

Currently this is my form builder function:

function _molmeth_add_form($form_state) {
  $form = array();

  // This is where form elements etc. get added                                                                                                 
  $form['adding_textarea'] = array(
                                     '#type' => 'textarea',
                                     );
  $form['add_button'] = array(
                             '#type' => 'submit',
                             '#value' => t('Add protocol'),
                             );

  return $form;
}

And this is my submit function:

function _molmeth_form_submit($form, &$form_state) {

  db_set_active('protocols');

  db_insert('trashy')
    ->fields(array(
                   'test' => 2,
                   ))
    ->execute();
  drupal_set_message(t('Your form has been saved.'));

  db_set_active('default');
}

I am neither sure if the function actually gets called nor if the code inside the function actually works.

Comments

hemant_gautam’s picture

I am using the same function which you have written, i have just changed the module name with test.
For testing purpose,weather the submit function is getting called or not.I am printing dummy text.
Its working for me.


/**
 * Implements hook_menu().
 */
function test_menu() {
  $items['test/form'] = array(
    'title' => 'Test Form',
    'page callback' => 'drupal_get_form',
	'page arguments' => array('test_form'),
    'access callback' => 'user_access',
  );
  return $items;  
}

function test_form($form_state) {
 $form = array();

  // This is where form elements etc. get added                                                                                                
  $form['adding_textarea'] = array(
                                     '#type' => 'textarea',
                                     );
  $form['add_button'] = array(
                             '#type' => 'submit',
                             '#value' => t('Add protocol'),
                             );

  return $form;
}
function test_form_submit($form, &$form_state) {
echo "here";die;
  db_set_active('protocols');

  db_insert('trashy')
    ->fields(array(
                   'test' => 2,
                   ))
    ->execute();
  drupal_set_message(t('Your form has been saved.'));

  db_set_active('default');
}
numfar85’s picture

Turns out it wasn't being called, I had to change the name of the submit function for it to work.

Thanks for the

echo "here";die;

it was very useful.