0

I have a multi-value text field, the values should be set from javascript on the browser.

For a single value text field, I can just set the value of the hidden field. But for multi-value text field, drupal will only take the first value and omit following values even the browser post more than one value.

I insert some input tag with same name (nearly, see below) under the form using jquery:

<input type="hidden" name="field_subscription_id[und][0][value]" value="52e1c1d99c29ec6fd84698b6" />
<input type="hidden" name="field_subscription_id[und][1][value]" value="52e1c1d99c29ec6fd84698b6" />
...

data is POST like this (inspect using dev tool of chrome):

field_subscription_id[und][0][value]=52e1c1d99c29ec6fd84698b6
field_subscription_id[und][1][value]=52e3de349c29ec6fd84698d4
...

After node created, the field field_subscription_id will only have the first value.

So, any idea to make this work? ~thx

EDIT

I define the form from UI, just a normal content type and a text field. I make a ajax request to external site from page, let user select something (from the ajax response), then fill a field of add-node form with the selected ids.

Using the AJAX response, I build a list of <li>, each <li> has a checkbox, the form attribute (HTML5) of the checkbox has been set to the value of form-id of drupal add-node form on this page.

  • Please provide more informations eg. what did you on drupal-side, are you using the form_api, how is your form defined etc.? – Wolf Feb 10 '14 at 14:58
  • I define the form from UI, just a normal content type and a text field. I make a ajax request to external site from page, let user select something (from the ajax response), then fill a field of add-node form with the selected ids. – clark Feb 12 '14 at 15:14
1

Fields will only appear in the $field_state array if they were there when the form was built. (If you inspect the $_POST array directly you should see your values there.)

The solution: When creating the form add enough fields so you don't need to add any more with js.

If you didn't create the form yourself then use hook_form_alter to add more fields.

If you need to be able to add more fields on the fly then you will have to look into the #ajax property. The examples module has some good examples on that.

Also make sure to use the #default_value, not #value property if you need to set a default value for any of the hidden fields as using #value will lock the value to that and stop the client from being able to make any changes.

  • I will checkout that module. BTW, if I want to inspect the $_POST array, which function should I look? – clark Feb 12 '14 at 15:31
  • If you have devel installed then dmp($_POST) will do the trick. If not then print_r($_POST) will also do it. – Felix Eve Feb 12 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.