469,337 Members | 6,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,337 developers. It's quick & easy.

Multiple data sets in a form

11
For certain reason, I need to check the validity of a form when submit with many data with shared name. The form looks like this:

[HTML]<form id="upload_form" name="upload_form" onsubmit="return uploadCheckValid2()">
<table>
<tr>
<td>xxx</td>
</tr>
<tr>
<td>
<input type="text" name="title[]" value="10" />
</label></td>
....
....
</form>[/HTML]

For the js:
Expand|Select|Wrap|Line Numbers
  1. function uploadCheckValid2()
  2. {
  3.     var titles = document.forms[0].elements['title'][0].value;
  4.     alert(titles);
  5.         if(xxx != xxx)
  6.     return false;
  7. }
I don't even able to alert the nodeName or value. Could anyone help??
Mar 17 '08 #1
11 1460
gits
5,390 Expert Mod 4TB
give your input-nodes an id and refer with:

Expand|Select|Wrap|Line Numbers
  1. var node = document.getElementById('the_id');
kind regards
Mar 17 '08 #2
hkma08
11
i have 10 inputs per table, and will have 5 tables in max. So your way works but will be very long in code. Any other ways?
Mar 17 '08 #3
gits
5,390 Expert Mod 4TB
do you mean you want to check all input nodes for the same thing? then just use:

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByTagName('input');
and check for the type == text and its values ...

kind regards
Mar 17 '08 #4
hkma08
11
Although the name attached to me is newbie, i don't think this is that easy to solve, and probably this is advance stuff.

May be i should explain more further on what i want to do. Did you notice that the name i gave to the input has braces? such as name="title[]". I have a table with following inputs:

title, author, isbn, post date etc...

when i hit an 'add' button, the table is duplicated with same structure, including names and ids. And in order to save to database by php, i have to use name="title[ ]" instead of just name="title" (w/o braces), since there is multiple record sets. When i was doing w/o multiple data sets, i used:

document.forms[' myform '][' title ']
or other ways such as the way you mentioned.

I tried and stuck on that the following ways do not work at all:
document.forms[' myform '][' title[ ] ']
or for (i=0;i<5;i++) { document.forms[' myform '][' title '][ i ] .......... }

simple example is that i have several isbn, all used share name, isbn[], the data pass to php is array of data, i am able to extract it to php, but not able to check every isbn is 10 or 13 digits (according to international book numbering) by javascript.

Get it? Excuse me for this complicated problem. But I am getting crazy on this ........ Please help if possible.
Mar 18 '08 #5
acoder
16,027 Expert Mod 8TB
How about document.getElementsByName("title[]") ?
Mar 19 '08 #6
hkma08
11
i tried this, doesn't work either...

var form = document.getElementById(formid);
var titles = form.getElementsByName('title[]');
alert(titles[0].value);
Mar 23 '08 #7
acoder
16,027 Expert Mod 8TB
getElementsByName is a method of the Document object. Try:
Expand|Select|Wrap|Line Numbers
  1. var titles = form.elements['title[]'];
instead.
Mar 24 '08 #8
hkma08
11
oh, i finally got it in some way.

Expand|Select|Wrap|Line Numbers
  1. function checkBookUploadValid(theForm)
  2. {
  3.     var inputs = theForm.getElementsByTagName('input');
  4.     for (var i=0; i<inputs.length; i++)
  5.     {
  6.         if (inputs[i].name == 'title[]')
  7.         {
  8.             if(inputs[i].value == '') {inputs[i].style.background = error_color; return false;}
  9.             else {inputs[i].style.background = 'white';}
  10.         }
  11. .....
  12. .....
  13. .....
  14. }
  15.  
This way works good. But still thanks to you guys trying to answer me..
Mar 26 '08 #9
acoder
16,027 Expert Mod 8TB
That's similar to what gits suggested, but anyway I'm glad you've managed to solve it.
Mar 27 '08 #10
hkma08
11
oh it is!
probably i was so stuck on something and misunderstand what he said.
my bad.
Mar 28 '08 #11
acoder
16,027 Expert Mod 8TB
Hey, no problem. At least it's working and that's what we're here for.
Mar 31 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by scott | last post: by
5 posts views Thread by Stanley Sinclair | last post: by
4 posts views Thread by William Wisnieski | last post: by
14 posts views Thread by =?Utf-8?B?UHVjY2E=?= | last post: by
2 posts views Thread by =?Utf-8?B?VGVycnk=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.