Hello,

I'm new to Drupal development but quite experienced with PHP in general, so I haven't quite yet figured out the "Drupal way" of doing things. Specifically, the Form API has been giving me some headaches. I realize, of course, that it's probably an issue of my understanding of the API.

I have a basic, static form. I create a $fields array and pass it off to drupal_get_form. Everything renders fine, and the form submits back to itself. My understanding is that drupal_get_form reads from $_POST directly and fills in the value accordingly. However, there is one extra 302 redirect at the end and so the $_POST variable gets clobbered. So, when the form is rendered after submission, none of the values are there to be inserted.

What am I doing wrong?

Thanks,
Jesse

Comments

profix898’s picture

Sorry, but I'm not sure I understand what exactly your issue is about!? You wrote "form submits back to itself", what to you mean? What are you trying to do? Are you writing a module? If so you generally have a function generating the form and a second function (submit handler) which actually operates on the submitted values/content. No need to play with $_POST directly. Are we talking about Drupal 4.7 or 5.0(cvs)?
You can take a look at the example.module to get a basic idea how the Drupal way looks like or read the Module developer's guide (http://drupal.org/node/508).

farmerje’s picture

I mean that the form submits to the same page on which the form is located, i.e., the form is on page /foo and has action="/foo".

When I submit, irrespective of what I put in _submit(), there is one extra redirection. The flow seems to be as follows:

Empty form --> deal with form data --> some other page

However, the page the user will ultimately be viewing is the same page on which the form is located. Because of the extra redirection the data from $_POST is lost and the form does not remember its values.

profix898’s picture

Ok, but I still cant find a problem with that. You can also redirect to another page in your _submit() handler. For example try to edit a term in a vocabulary and you will directed back to the vocabulary instead to the term edit page. What hurts? The page you are directed to (regardless being the same the form was on or any other) will be generated again as if you were calling it the first time. Thats handled by the menu system and hook_menu(). All required/important values from the form should have been save in the _submit() handler, so you have them when you create the page again. Not!?

farmerje’s picture

I don't know, that's why I'm asking. Like I said, I'm new to Drupal.

So the usual way to have a form that remembers what values it submitted if it's submitting to itself is to save off the variables in the _submit handler?

profix898’s picture

You are welcome to ask everything you need to know ... but I need to understand what you are asking ;)

Yes, the general case is to save all data in the _submit() handlers. Drupal never assumes that you are returning to the same page. But what do you need that for? I can hardly image a case where you need the same form again without the values being saved. Is there a real problem you are facing with? Can you provide a code snippet we can discuss this on? Or are you just curious?

twom’s picture

Hy,

I had the same problem once, and it drove me crazy :)

look at this post. I hope this gives the answer to your question!

(basically: add $form['#redirect'] = FALSE; to your form

farmerje’s picture

Sorry, look at what post?

profix898’s picture

twom’s picture

yes thanks, i didn't pay attention: I meant http://drupal.org/node/56129

jasan’s picture

Thank You,You Really Saved My Time