Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP + Javascript + Checkboxs

Status
Not open for further replies.

bjblackmore

Programmer
Jul 10, 2002
5
GB
Hi
I have the following javascript...

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
var checkflag = &quot;false&quot;;
function check(field) {
if (checkflag == &quot;false&quot;) {
for (i = 0; i < field.length; i++) {
field.checked = true;}
checkflag = &quot;true&quot;; }
else {
for (i = 0; i < field.length; i++) {
field.checked = false; }
checkflag = &quot;false&quot;; }
}
</script>

executed using..

<input type=checkbox value=&quot;true&quot; onClick=&quot;check()&quot; name=&quot;delete&quot;>

However it does work when I have multiple checkboxs in the form of arrays i.e.

<input type=&quot;checkbox&quot; name=&quot;checkbox[0]&quot; value=&quot;abc&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox[1]&quot; value=&quot;def&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox[2]&quot; value=&quot;ghi&quot;>

Does anyone know how to get the javascript to recognise checkbox[]?

Many Thanks

Ben
 
try using:
in the script...
var checkbox=new Array(checkbox0,checkbox1,checkbox2);

and then...
<input type=&quot;checkbox&quot; name=&quot;checkbox0&quot; value=&quot;abc&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox1&quot; value=&quot;def&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox2&quot; value=&quot;ghi&quot;>
instead of...
<input type=&quot;checkbox&quot; name=&quot;checkbox[0]&quot; value=&quot;abc&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox[1]&quot; value=&quot;def&quot;>
<input type=&quot;checkbox&quot; name=&quot;checkbox[2]&quot; value=&quot;ghi&quot;>

Might not be perfect, but get the idea?

Rick If I have helped you just click the first link below to let me know :)
 
In JavaScript, use form.elements array to check on your checkboxes.

[tt]
Code:
<html>
<head>
	<script language=javascript>
		var checkname = &quot;barney[]&quot;;
		
		function count_boxes ()
		{
			var count = 0;
			for (counter = 0; counter < fred.elements.length; counter++)
			{
				if (fred.elements[counter].type == &quot;checkbox&quot;)
				{
					if (fred.elements[counter].name == checkname)
					{
						if (fred.elements[counter].checked)
						{
							count++;
						}
					}
				}
			}
			confirm (&quot;There are &quot; + count + &quot; boxes checked&quot;);
		}
	</script>
</head>
<body>
	<form name=fred>
		<input type=checkbox name=&quot;barney[]&quot; value=&quot;a&quot;><br>
		<input type=checkbox name=&quot;barney[]&quot; value=&quot;b&quot;><br>
		<input type=checkbox name=&quot;barney[]&quot; value=&quot;c&quot;><br>
		<input type=checkbox name=&quot;barney[]&quot; value=&quot;d&quot;><br>
		<input type=checkbox name=&quot;barney[]&quot; value=&quot;e&quot;><br>
		<input type=submit>
	</form>
	<a href=&quot;javascript: count_boxes();&quot;>count</a>
</body>
</html>
[/tt]
 
Um, sleipnir214, your sig has already been copywrited. Sorry, give it back! ;-)


Rick If I have helped you just click the first link below to let me know :)
 
My signature is in a transitional phase right now. I used to have one that blatantly told you to vote to give the star.

Then Dave Murphy suggested that it would be best if my signature were more subtle. I noodled with the idea of using &quot;Dave Murphy says I'm not supposed to ask you for a star&quot; for a while, but decided against it.
 
Ok. That's fine. I like the Dave Murphy idea though!!! jk. I would totally agree and never give you a star!!! Just kidding.

Rick If I have helped you just click the first link below to let me know :)
 
sleipnir214, your post said use var checkname = &quot;barney[]&quot;; with <input type=checkbox name=&quot;barney[]&quot; value=&quot;a&quot;>, <input type=checkbox name=&quot;barney[]&quot; value=&quot;b&quot;>..etc would it still work if checkbox name was barney[0], barney[1], etc?
 
I have another way of doing the checkall here is a working example :

<html>
<head>
<title>check all</title>
<script>

function checkAll(form, flag)
{
var checkboxes = form.getElementsByTagName(&quot;input&quot;);
var len = checkboxes.length;

while(len--)
{
checkboxes[len].checked = flag;
}
}
</script>
</head>
<body>

<form id=myForm>
<input type=checkbox onclick=&quot;checkAll(document.getElementById('myForm'), this.checked)&quot;>

<input type=checkbox>
<input type=checkbox>
<input type=checkbox>
<input type=checkbox>
<input type=checkbox>
<input type=checkbox>
<input type=checkbox>
</form>
</body>
</html> Gary Haran
 
i use this:

function DoCheckBoxes() {
df = document.forms[0];
for (ele=0;ele<df.length;ele++) {
part = df.elements[ele];
if (part.type == &quot;checkbox&quot;) {
if (part.checked != true) {
part.value = &quot;False&quot;;
part.checked = true;
}
}
}
}

Run that onsubmit and it will check all of the unchecked boxes and give them a value of False --BB
 
bjblackmore,

It would not work if you used numbers in the brackets. But you wanted the checked values returned in a single array to PHP didn't you? You don't need the numbers if you want the data returned to PHP in a single array. Just naming them all &quot;barney[]&quot; ensures that the checked values will be made available in the array $_POST[&quot;barney&quot;].



xutopia, will your solution work with browsers other than IE? ______________________________________________________________________
If this post was helpful,
click the link below
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top