By zhangx1a0 on
I have create a form by using form API and there is a hidden type element. I have to submit buttons in the form, one is 'save' and another is 'test'. When clicking 'test' I want change the hidden element value and submit to the form page, just look like nothing changed in the form page but just the hidden value changed. How to change the hidden value?
In the submit function I try to change it by using $form_values['xxxx'] = 'xxxxxxx'; but it does not work...
Comments
It's not clear just what you're trying,
you might want to post your code to make it clearer.
me too. FAPI is being too safe.
I have this problem also.
I have a hidden field in the form, and on the UI I manipulate the value of that field (using javascript) to send something different back.
On submit, the FORM API decides that seeing as the field was hidden, the user should not have been messing with it, and sets the value back to default before I can process it.
Inspecting the $_POST shows the value was changed and sent, but inspecting the $form_values shows it was ignored.
I'm thinking of trying the SKIP_SAFETY_CHECK or whatever that thing is called.
(FWIW, my hidden field is a response to some dynamic form manipulation where I want to delete a chunk of fields, and leave a 'deleted' flag behind to let my system know it's gone, not just ignored. )
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
in validate?
Just flagging that change the value in the 'validate' op (and perhaps others??) won't stick, you need to use form_set_value(). I'm not 100% sure this is your issue, but I thought I'd mention it.
That's true, but even at the
That's true, but even at the validate phase, the $form_values passed in don't show the true form fields submitted. They've already been sanitized, and hidden fields reset. I believe.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
yes that is different
Well, I'm glad I know about that before it bites me. :-)
You need to directly access the $_POST array in validating
Yes, the values are sanitized, so the cleanest way I know of is to assign the values you want from the changed hidden fields by reading them from the $_POST array in a _validate hook. Then set your value with form_set_value(), http://drupal.org/node/51104, http://api.drupal.org/api/head/function/form_set_value.
Use textfield instead of hidden
Because hidden uses #value instead of #default_value, the user cannot change the value - this concerns javascript too.
So I used an hidden textfield for my needs (I need it for saving values of multiple select fields in one string):
This works fine and you don't have to change the values in submit and you are able to look at them in preview.
Is this still an issue?
I found that even if I set a hidden form value when the form is built, the value can get overridden at submission if the form is loaded by another user and the hidden form value is different for that user!
I was going to submit a bug but I found this on version 5.1 and therefore could have been fixed, or could be an issue cos I'm working on a hacked Drupal install :(.
Can anyone confirm this issue still exists in the latest 5.x?
==========
www.e3m.co.uk
Still happens
But i found the easiest solution for my needs was to define the form field with no value. Simply:
$form['myHiddenField'] = array('#type' => 'hidden');
Then I set the values by javascript and it works the way I expect.
This worked for me too.
This worked for me too. Thanks!
Works!
Great solution. Thank you.
As per the Form API docs: If
As per the Form API docs: If you use #value, that value will be reset. To place a value in a hidden field and allow it to be replaced by javascript, use #default_value.
http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc...
IT WORKS!
@mattconnolly Thanks dude!
Your answer was the clearest one!
Sometimes it's such a pain in the a** when you don't read between the lines in the docs :/
Thanks again! :D
--
Heitor Althmann
Drupal Developer
My Bad
My Bad, Tried all of the given options, but still i am not able to print the hidden value in array, after submitting the form.
Code here :
var children = $('input:radio[name=do_you_have_children]:');
children.change(function () {
if ($('input:radio[name=do_you_have_children]:checked').val() === 'Yes') {
appendHiddenVal();
}
function appendHiddenVal(){
console.log('added---')
$('#edit-baby-first-name').on('blur', function() {
fnameVl = $(this).val()
alert(fnameVl);
appendHidden = $('').attr({
type: 'text',
id: 'foo',
name: 'baby_first_name11',
value: fnameVl
}).appendTo('#my-form');
console.log("appendHidden --- " + appendHidden);
});
}
// PHP code from my submit function
function my_form_submit(&$form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
$form_state['no_cache'] = TRUE;
<--- data storage logic -->
}
Any Other solution..
Thanks!!!
You can't add form elements
You can't add form elements using javascript in Drupal. The Form API will reject the value if it wasn't part of the form when the form was generated. So you need to either pass the hidden field when you are generating the form, and then change the value using javascript, or you need to use #ajax in the Form API to add new form elements.
Contact me to contract me for D7 -> D10/11 migrations.