Accessing HTML checkboxes with php and javascript

Reply

Join Date: Aug 2006
Posts: 19
Reputation: tmv105 is an unknown quantity at this point 
Solved Threads: 0
tmv105 tmv105 is offline Offline
Newbie Poster

Accessing HTML checkboxes with php and javascript

 
0
  #1
Jan 11th, 2007
Hi! I am having problems validating my HTML to at least transitional due to my use of checkboxes and accessing them with PHP and javascript. I want client-side validation and server side functionality and therefore am utilizing PHP and JS.

My understanding is that JS recognizes checkboxes to be an array by repeating the "name" attribute in multiple input tags. PHP recognizes checkboxes to be an array by repeating the "name" attribute followed by an empty subscript. name[]. In order to get both PHP and javascript to recognize i've set all my id attributes to the same name and all my name attributes to the same name plus the []. Here's some of my code.
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 8:00 a.m." /> 8:00 a.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 9:00 a.m." /> 9:00 a.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 10:00 a.m." /> 10:00 a.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 11:00 a.m." /> 11:00 a.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 12:00 p.m." /> 12:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 1:00 p.m." /> 1:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 2:00 p.m." /> 2:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 3:00 p.m." /> 3:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 4:00 p.m." /> 4:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, 5:00 p.m." /> 5:00 p.m.<br />
<input type="checkbox" id="availcheck" name="availcheck[]" value="Monday, evening" /> 6:00 - 9:00 p.m.

Obviously, the repetition of the id attribute is a violation for validation. Any suggestions on what I could do?

Thanks in advance for any suggestions.

Tara
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: Accessing HTML checkboxes with php and javascript

 
0
  #2
Jan 11th, 2007
Do not repeat the id. You can access the fields by name in JS, ex. getElementsByName('availcheck[]')
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Accessing HTML checkboxes with php and javascript

 
0
  #3
Jan 13th, 2007
getElementByName() is not supported on all browsers.

The DOM functions getElementsByTagName and getAttribute are more supported.


Try a function like which uses those two to get the same result:

  1.  
  2. /**
  3. * Retrive an HTML Element Collection By name
  4. * @return Array HTML Elements
  5. * @param Object Parent Element
  6. * @param String name
  7. * @param String Tag name to limit search to
  8. */
  9. function getElementsByName(parent, name, tag) {
  10.  
  11. var els = new Array();
  12. var allChildren = parent.getElementsByTagName(tag || '*');
  13.  
  14. for (var i = 0; i < allChildren.length; i++) {
  15. var child = allChildren[i];
  16. if (child.getAttribute('name') == name) {
  17. els.push(child);
  18. }
  19. }
  20.  
  21. return els;
  22. }
Last edited by digital-ether; Jan 13th, 2007 at 12:41 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC