944,023 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 10198
  • PHP RSS
Jan 11th, 2007
0

Accessing HTML checkboxes with php and javascript

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tmv105 is offline Offline
22 posts
since Aug 2006
Jan 11th, 2007
0

Re: Accessing HTML checkboxes with php and javascript

Do not repeat the id. You can access the fields by name in JS, ex. getElementsByName('availcheck[]')
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Jan 13th, 2007
0

Re: Accessing HTML checkboxes with php and javascript

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:

PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Matrix Script
Next Thread in PHP Forum Timeline: How to best rank items based on vote data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC