Hello,

I can't think of a way to fix the following problem:

I have this menu item that will catch all requests to www.example.com/search/% [whatever]

function search_menu(){
  $items['search/%'] = array(
    'title'  => t('Search Results'),
    'page callback' => 'search_main',
    'access arguments' => array('access content'),
    'file' => 'search.inc',
    );
}

And have this form that will submit my form to www.example.com/search/cards [or whatever...]

function search_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'),
  );
}

I know the form is working properly and that search_main() function is handling the form submission because I'll get the correct data if I do:

function search_main(){
  print_r($_POST);
  die();

  return 'Just searched: '. $_POST['what'];
}

However, without the die() at search_main() function, it seems the function is called again (this time $_POST will be an empty array) and all my data handling is lost...

What exactly is happening? How can I fix it to the desired behaviour (being the desired behaviour the display of the return value with the posted info) ?

Thanks in advance!
ACM.

Comments

jason_gates’s picture

Remove $form['#action'] = url('search/cards');. You handle form submissions with a "submit handler" not a menu callback.

Check the quick start guide here http://drupal.org/node/751826. Search for the term "Submitting Forms". There is an example of a "submit handler".

jason_gates’s picture

Remove $form['#action'] = url('search/cards');. You handle form submissions with a "submit handler" not a menu callback.

Check the quick start guide here http://drupal.org/node/751826. Search for the term "Submitting Forms". There is an example of a "submit handler".

iamPhoenix’s picture

Hi Jason, thanks for your reply.

If I remove $form['#action'] = url('search/cards'); the form will be expecting search_submit() function at the same URL, right?

I really need to submit my form to 'search/cards' and handle the form there. How can I do it?

jason_gates’s picture

'search/cards' is mapped to a method, right? Just call that method in your submit handler or issue a redirect.

iamPhoenix’s picture

Hi Jason, thanks again for your effort.

'search/cards' is indeed mapped to search_main() as can be seen at search_menu() function. I can't just call the method from the form_submit() handler since that won't post the form to www.example.com/search/whatever as expected.

The redirect will make $_POST = array() and that's exactly what I'm trying to avoid.

Maybe I'm not explaining correctly what I'm trying to do. What I want is a form that will respect this flow:

  1. At www.example.com/search lays the form
  2. The form submission will post the information into www.example.com/search/whatever
  3. At www.example.com/search/whatever the form submitted values will be handled
  4. As the final step, still at www.example.com/search/whatever, print the handled response

The problem is that, using the code as stated above, somewhere between step 3 and 4, my $_POST becomes array() and I cannot display what I posted.

If I keep the die(); I can see my posted information but obviously nothing else works.

I hope it's easier to understand this time.

Thanks again,
ACM.

roynilanjan’s picture

This is a problem with form handler even you change the submit handler e.g like

$form['#submit'][] = 'search_submit';
but still need to null or unset the $form['action']
otherwise submit handler will not-work!

Do you have any suggestion!

jason_gates’s picture

With all due respect you didn't give any reason or explanation for "I really need to submit my form to 'search/cards' and handle the form there. How can I do it?".

You don't handle Drupal form submissions with a menu callback? You don't post a Drupal form to a menu callback.

So again, you made the assertion without any explanation or rationalization, that "I really need to submit my form to 'search/cards' ". Ok why??

What purpose does then menu callback serve? Per your code, zero, nothing, not necessary and serves no purpose.

One more time, handle the form submission with a submit handler, not a menu callback.

In your submission handler you retrieve the user entered values (again, don't post a form to a menu callback, use the standard submit handler). Perform whatever logic you need in the submit handler and then generate a response.

If you want generate a response by redirecting to a menu callback (again the form is not submitted to the menu callback, the user's request is redirected to the menu callback), use either a "#redirect" attribute in your form definition of a drupal_goto().

To summarize, you do not post Drupal forms to a menu callback. Look at the title of your post, what question does it pose. Now did I answer that or not?

iamPhoenix’s picture

Hi Jason,

It's not my intention to upset you. Probably this is a weird question and I keep coming with weak arguments.

I'll try to answer properly this time.

You don't handle Drupal form submissions with a menu callback? You don't post a Drupal form to a menu callback.

So again, you made the assertion without any explanation or rationalization, that "I really need to submit my form to 'search/cards' ". Ok why??

What purpose does then menu callback serve? Per your code, zero, nothing, not necessary and serves no purpose.

I thought the only way I could submit the form to 'search/cards' ($form['#action'] = 'search/cards') without getting a drupal_not_found() message, was having the menu callback. Also, although I wanted people to POST form to 'search/cards' I would also like to allow people to write directly at URL 'search/cards/12345', so there is the reason for having the menu callback.

At this point (besides thinking I'm stupid), you may ask: people can write directly to url, what's the problem with #redirect at form submit handler? Nothing special... I dare to say, none! Just wanted to know if it was possible to submit to other URL (defined by menu callback) and handle it there.
That was my question. If it was possible to POST forms to other page without using redirects or gotos? (After using a redirect or goto it's not a POST anymore, right?)
The conclusion is: As long as you don't need to print any kind of response, it's possible yet not recommended and prone to give weird results.

The best way is (using your own words):

(...) handle the form submission with a submit handler, not a menu callback. (...) generate a response by redirecting to a menu callback (...)

Got it. That's the way Drupal works. Will do.

Thank you very much for your time and precious help Jason,
ACM.