Jump to content

Retrieving data from form arrays


Thierry

Recommended Posts

I have a form in which several fields share the same name via an array (<input name="cookie[]" id="cookie[]">).

 

I've been trying to use 'document.getElementById("cookie")[0]' and also 'document.getElementById("cookie[0]")', but neither yield any results.

 

Anyway of getting the field object while its a form array? (The error I get: "Object expected");

Link to comment
Share on other sites

name="cookie[]"

 

the name is cookie[] not the id, getElementById is only for searching elements with id's.

and you may be tempted now to try set id's for all your input elements called 'cookie[]', but ids have to be unique (cant have many elements with all their ids called 'cookie[]');

hence why u must use the name attribute like you are doing so php knows its an array when you submit...

 

You need to access the values via getting the elements in the form.

document.forms[0]['cookie[]']

or

document.forms['nameOfForm']['cookie[]']

 

or

document.forms[0].elements['cookie[]']

or

document.forms['nameOfForm'].elements['cookie[]']

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.