954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing HTML checkboxes with php and javascript

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.
8:00 a.m.
9:00 a.m.
10:00 a.m.
11:00 a.m.
12:00 p.m.
1:00 p.m.
2:00 p.m.
3:00 p.m.
4:00 p.m.
5:00 p.m.
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

tmv105
Newbie Poster
22 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Do not repeat the id. You can access the fields by name in JS, ex. getElementsByName('availcheck[]')

php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 

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:

/**
* Retrive an HTML Element Collection By name
* @return Array HTML Elements
* @param Object Parent Element
* @param String name
* @param String Tag name to limit search to
*/
function getElementsByName(parent, name, tag) {

	var els = new Array();
	var allChildren = parent.getElementsByTagName(tag || '*');
	
	for (var i = 0; i < allChildren.length; i++) {
		var child = allChildren[i];
		if (child.getAttribute('name') == name) {
			els.push(child);
		}
	}
	
	return els;
}
digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You