...

View Full Version : number of checkbox selected



katherine06
02-15-2006, 06:45 AM
i have a code that should trap that only 1 checkbox should be selected but i don't know how????? any suggestions please please plase???
thanks in advance :)

StupidRalph
02-15-2006, 06:53 AM
Do you mean that you are looking for a script to check and make sure that only one check box is checked?

If so I'd recommend a client-side language (javascript) to do so as to not tie up my server. But it can be done in PHP as well.

Create an array by naming all the check boxes the same name and then run a for loop to check which ones have a value of being checked.

OR

You could simply use radio buttons :D

degsy
02-15-2006, 02:53 PM
If so I'd recommend a client-side language (javascript) to do so as to not tie up my server.
You need to do it in PHP. Javascript is just a nice for the user, but if JS is disabled then it could break your script.

Here is an example
http://degs.co.uk/test/hct/loop_through_checkbox_check_limit_php_array.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script>
function check(){
count = 0;
str = '';
for(x=0; x<document.form1.elements["checkbox[]"].length; x++){
if(document.form1.elements["checkbox[]"][x].checked==true){
str += document.form1.elements["checkbox[]"][x].value + ',';
count++;
}
}

if(count==0){
alert("You must choose at least 1");
return false;
}
else if(count>3){
alert("You can choose a maximum of 3");
return false;
}
else {
alert("You chose " + count + ": " + str.substring(0,str.length-1));
document.form1.submit();
}
}
</script>
</head>

<body>
<?php
if(isset($_POST['Submit'])){
if(is_array($_POST['checkbox'])){
echo "You chose: <br>";

foreach($_POST['checkbox'] as $key => $val){
echo "$key: $val <br />";
}
}
else{
echo "Something went wrong. Try again.";
}
}
?>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<p>
<input type="checkbox" name="checkbox[]" value="red" />
Red<br />
<input type="checkbox" name="checkbox[]" value="green" />
Green<br />
<input type="checkbox" name="checkbox[]" value="blue" />
Blue<br />
<input type="checkbox" name="checkbox[]" value="black" />
Black<br />
<input type="checkbox" name="checkbox[]" value="white" />
White<br />
<input type="checkbox" name="checkbox[]" value="yellow" />
Yellow<br />
<input type="checkbox" name="checkbox[]" value="purple" />
Purple
</p>
<p>
<input type="submit" name="Submit" value="Click Me" />
</p>
</form>
</body>
</html>

katherine06
02-16-2006, 02:37 AM
:) :) :)

katherine06
02-16-2006, 02:40 AM
y thank you for the idea!thanks a lot for giving me an example.:thumbsup:
It is in a different concept but it did help me to have other idea! Thanks a lot p&#246;!!!!! God Bless!!!! :)


You could simply use radio buttons :D

err..i have a check all, delete, and edit buttons,.i choose checkboxes so the user can select all the checkboxes he wants to delete and choose only one what he wants to edit..but still thank you for the concern :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum