php – How can I submit my Drupal form to any other internal page? – Education Career Blog

I’m having a problem with Drupal forms. Can’t think of a way to solve it and I was wondering if any brain out there has the solution to my problem.

I have this form:

function mymodule_myform(){
  $form'#action' = url('search/cards');
  $form'whatwhere''what' = array(
    '#type' => 'textfield',
    '#title' => t('What?'),
    '#maxlength' => 80,
    '#required' => TRUE,
  $form'submit' = array(
    '#type' => 'submit',
    '#value' => t('Search'),

Which, as you can see, is supposed to submit all info to
This will indeed submit my form to the desired URL. However, without the mymodule_menu() hook defining the path, it will end in a 404.

So I add:

function mymodule_menu(){
  $items'search/%' = array(
    'title'  => t('Search Results'),
    'page callback' => 'mymodule_main',
    'access arguments' => array('access content'),
    'file' => '',

And, at file I’ll create my function mymodule_main():

function mymodule_main(){
  die(); // ### Note the die(); ###

  return 'Just searched: '. $_POST'what';

If I leave it exactly like it is, of course PHP will end the script execution right after printing my form info on the screen. All good!

However, if I remove the die(), it seems the function is called twice, and the second call does not have $_POST filled up anymore.. Seems mymodule_menu() overrides in some way whatever the form submit handler is doing…

The question is: How can I submit my form to any other internal page without having the 404 and keep my form info?

Thanks in advance.


I think you need to use the method described in

function MODULE_block_form_submit($form, &$form_state)  
    if (isset($_REQUEST'destination')) {  

    if (isset($_REQUEST'edit''destination')) {  

    $form_state'redirect' = 'search/cards/'. trim($form_state'values''whatwhere');  


I don’t know exactly, but maybe your code conflicts with drupal’s builtin search module?


After some debate @ drupal forums (which can be followed here) conclusion is that: it is not possible to have a menu callback handling form results. You can instead, use a form submit handler (as always) and then redirect to your menu callback.
It’s not a POST to the menu callback but it’s as good as it gets.

Thanks for your help.

Leave a Comment