Community

Form submit handler not working when using drupal_get_form()

Hi. In a drupal 6 custom module I built, I have a menu callback which contains this:

<?php
function mymodule_register_confirmation() {
 
$content = drupal_get_form('mymodule_newsletter_form');
  echo
theme('page', $content);
}
?>

I have the function that returns the form:

<?php
function mymodule_newsletter_form(&$form_state) {
 
$form = array();

 
$form['logo'] = array(
   
'#type' => 'markup',
   
'#value' => '<div class="logo-header">',
  );
 
  ...

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

  return
$form;
}
?>

I also built the function for the submit handler of newsletter form:

<?php
function mymodule_newsletter_form_submit($form, &$form_state) {
    die(
'I am in the submit handler!!');
}
?>

The page shows as expected and contains the newsletter form exactly as I want. However, when I click submit button, the submit handler I built never executes.
I even tried adding the following to the function that returns the form:

<?php
    $form
['submit']['#executes_submit_callback'] = TRUE;
   
$form['#submit'][] = 'mymodule_newsletter_form_submit';
?>

What am I doing wrong?

Thanks in advance.

Comments

Try replacing 'die' with

Try replacing 'die' with 'drupal_set_message'

Still doesn't work

I did the replace but it still not executing the submit handler.
I even put a watchdog in there and it is not executing either.

Found the problem

Ahhhh, found the problem!

I had to specify manually a redirect to the form:

<?php
$form
['#redirect'] = ''; // Redirects to the home page
?>

This way the validate and submit hooks are called appropriately and then it redirects to the home page.

Thanks!!!

Cheers.

I tried the redirect method

I tried the redirect method but it still doesn't work for me. Are you sure that it's what making it to validate?

I'd say his problem lay with

I'd say his problem lay with echoing the form in the first function, rather than returning it. You don't echo things in Drupal, except in template files.

Jaypan We build websites