1

From my understanding, and as I've seen it in other examples of code, all I need to do to call a custom submit function is include the "#submit" => array("myModule_submit");.

I use a drupal_set_message to post a message on the site so I can know if the myModule_Submit is being called from the form submit button:

function myModule_submit($form, &$form_state) { 
  drupal_set_message($form_state['values']['name']);
}

When I click on the "Send" button nothing happens, and it seems the page just refreshes.

function myModule_form_builder($form, $form_state){
  $form['myModule_message'] = array(
    '#type' => 'textfield',
    '#title' => t("Outgoing Message"),
    '#size' => 150,
    '#maxlength' => 150,
    '#required' => TRUE,
    '#description' => t("Type your message"),
  );

  $form['buttons'] = array(
    '#type' => 'button',
    '#value' => t("Send"),
    '#submit' => array('myModule_submit'),
  );

  return $form;
}

function myModule_submit($form, &$form_state) {
  drupal_set_message($form_state['values']['name']);
}

To my understanding drupal_set_message() will display a message to the user on the current page somewhere given the parameters. When I put information in the fields and press submit though there are no messages. I have seen other examples on the web that use this method and they say it works.

migrated from stackoverflow.com May 14 '12 at 16:02

This question came from our site for professional and enthusiast programmers.

4

Try next:

function myModule_simple_form($form, &$form_state){
  $form['firstname'] = array(
    '#title' => t('First name'),
    '#type'  => 'textfield',
  );
  $form['lastname'] = array(
    '#title' => t('Last name'),
    '#type'  => 'textfield',
  );

  $form['submit'] = array(
    '#value' => t('Search'),
    '#type'  => 'submit',
  );
  return $form;
}
function myModule_simple_form_submit($form, &$form_state) {
  drupal_set_message($form_state['values']['firstname']);
}

Any rendered drupal form already have a submit function. Also try: drupal_set_message($form); in your hook_from_alter when $form_id = 'myModule_simple_form' too see where is default submit function. You can also add a custom submit funtion in array but it is necesary only if you need more than 1 submit function for your form. In all othre cases use just form_id_submit.

  • Can I expect the drupal_set_message() to appear on the form page after submit has been pressed or is it displayed elsewhere? – Eric May 12 '12 at 18:47
  • Yes. After submit before form. drupal_set_message('<pre>'.print_r($form_state, true).'</pre>'); will give you all $form_state array that is nicely formated. – Sergey Litvinenko May 12 '12 at 21:27
2

The only problem in your code was using '#type'=>'button' instead of '#type'=>'submit'

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.