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:

<?php
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 www.example.com/search/cards.
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:

<?php
function mymodule_menu(){
  $items'search/%' = array(
    'title'  => t('Search Results'),
    'page callback' => 'mymodule_main',
    'access arguments' => array('access content'),
    'file' => 'mymodule.inc',
    );
}
?>

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

<?php
function mymodule_main(){
  print_r($_POST);
  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 http://api.drupal.org/api/function/search_box_form_submit/6


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

    if (isset($_REQUEST'edit''destination')) {  
        unset($_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