views:

35

answers:

3

Hello,

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.

A: 

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

galymzhan
Nope. That's not the case, I have a unique module name. `Search` it's just used in this example context. Thanks anyway.
acmatos
+1  A: 
MPD
Hi MPD, as stated in the title, I want to SUBMIT (using POST) to other page, not just redirect with values at URL... Anyways, thanks for your help.
acmatos
A: 

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.

acmatos