In my multipart form validate function why do I see the expected value in the $_POST array but not in the $form_values array?

I have a hidden element that gets changed in the form and then posted via javascript with a call like this:

document.getElementById('hidElementname').value = '1';
document.getElementById('formname').submit();

So in my form validating function I see the new value of 1 in the $_POST array but the old value of 0 in the $form_values array. What's the deal here? This isn't rocket science... the core should be copying the value over to $form_values and be done with it. What am I missing here? This is causing all sorts of issues in my multipart form. Thanks for any help in advance.

Comments

evil_marty’s picture

because drupal forms only allow values which have been defined in the form generation phase. So if you had in your form generation a field named example then when the form was submitted that value would be returned and be available in form_values. The reason for this is a security mechanism which validates the integrity of the form data to ensure malicious data submitted to be disgarded. When it comes to dynamic forms and drupal 5, it gets a little complicated trying to work. Version 6 helps alot with that, which still keeps the security mechanism but with the ability to extend the form based on returned data.

sperna’s picture

But I am defining the hidden element in my drupal form generation. I'm only changing the value on the client side. Surely changing a value in the form element that was generated by drupal should be allowed.. right? If this was the case then a user couldn't enter text in to a textfield and submit it.

evil_marty’s picture

how are you defining the element in drupal? might be that its overwriting the returned value when the form is getting processed.

sperna’s picture

$form['addRows'] = array('#type' => 'hidden', '#value' => '0');

I just want to emit this in the html, have a user click on a + image which increments the value to 1 and performs a submit via javascript. The value is coming back as a 1 in $_POST but a 0 in form_values. The html looks fine at the client side.

evil_marty’s picture

are you referencing the same hidden value?

$form['addRows'] = array('#type' => 'hidden', '#value' => '0');

the field should have the id edit-addRows assigned by the form generation mechanism so in your javascript code it should be something like this:

document.getElementById('edit-addRows').value = '1'

or you may want to declare it using the attribute #id

if that doesn't work then you might need to trace the whole form processing procedure =( hopefully doesn't get that far.

sperna’s picture

Yeah I'm referencing it properly in the js with the mangled name. It's getting sent to the server and i'm seeing it in $_POST but not in $form_values. I just bagged it and i'm using $_POST. I've got no time for that nonsense working on a behind schedule project. Thanks for the suggestions.

rick20’s picture

Hi, I have exactly the same problem.
Have you found the solution yet?
I modify a hidden field value using jquery too but the value is not passed to $form_values.
Any idea?

thx b4

danieltome’s picture

I was having the same issue.
Change #value to #default_value

cheers,

Danno
http://www.danieltome.com

sequoiadrake’s picture

I had the same issue with regular text fields
#default_value is the property to set !

oallen’s picture

I can confirm this is the right solution. I was having the same problem. I still don't understand why setting #default_value is the right way to set values which may change on page load (such as those done with JS, or in my case a form field whose data comes from the $_GET array). However counter-intuitive it may seem, it worked.

mazz’s picture

Thank you, was causing me a huge headache hope anyone else having issues with $_POST and $form_values not matching finds this instead of having to rely on relatively unsanitized $_POST in their modules. Luckily in my case this code is behind admin panel so it's not a huge issue.

mooffie’s picture

document.getElementById('formname').submit();

Perhaps it's that problem: http://drupal.org/node/220887

crossmedia’s picture

I would like to add that $_POST[somevalue]; will work only if you created your form in your own custom module and calling this form through drupal_get_form(form); in any of your page, so in this case you would be able to get values through $_POST[somevalue]; not form_values[somevalue]. Please correct me if i am wrong.

[Note] i m talking about Drupal 6 not 5 here