SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Need help array validation
Hybrid View
-
Jul 21, 2007, 04:16 #1
- Join Date
- Jul 2007
- Posts
- 170
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Need help array validation
Hi, I have a form dynamic output by php like this:
<input name="field[36]" id="field[36]" type="text" size="1" class="textfield" value="2" />
<input name="field[36]" id="field[36]" type="text" size="1" class="textfield" value="2" />
<input name="field[80]" id="field[80]" type="text" size="1" class="textfield" value="1" />
<input name="field[110]" id="field[110]" type="text" size="1" class="textfield" value="1" />
while( list( $key, $value ) = each( $_POST['field'] ) )
-
Jul 21, 2007, 05:45 #2Code HTML4Strict:
<input name="field[]" type="text" size="1" class="textfield" value="2" /> <input name="field[]" type="text" size="1" class="textfield" value="2" /> <input name="field[]" type="text" size="1" class="textfield" value="1" /> <input name="field[]" type="text" size="1" class="textfield" value="1" /> <input type="button" onclick="checkAll()" /> <script type="text/javascript"> function checkAll(){ var fields=document.getElementsByName('field[]'); for(i=0; fields.length; i++){ alert(fields[i].value); } } </script>
This won't work on IE6 though; to make it work on obsolete browsers, you can loop through document.form_name.elements and check for element name.Saul
-
Jul 21, 2007, 06:09 #3
- Join Date
- Jul 2007
- Posts
- 170
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks daemon, but I can't do this 'field[]', I need the key intact as in field[110]. Is there anyway to do that?
-
Jul 21, 2007, 06:16 #4
OK, I will believe you must specify the indices. Then you could loop through the form elements and use substr to check the first characters of the name.
Code HTML4Strict:<script type="text/javascript"> function checkAll(){ var fields=document.myform.elements; for(i=0; fields.length; i++){ if(fields[i].name.substr(0,5)=='field'){ alert(fields[i].value); } } } </script>
Saul
Bookmarks