PDA

View Full Version : check box array validation?


bsxiong
06-04-2008, 11:19 AM
I just want an alert saying that they will need to select at least one of the box to continue.

anyhelp? I just can't get it to work.


<title>MANY VALIDATIONS</title>
<style type="text/css">
body {width:600px; padding:10px; font-family: verdana; font-size: 12px;}
</style>
<noscript>Javascript is currently turned off, Please turn it back on for this page to work.</noscript>
<script type="text/javascript">

function validate()
{
var computer2 = document.classroom.computer;
for($i = 0; $i < computer2.length; $i++){
alert(computer2[$i])
}

}


</script><form name=classroom method=post onSubmit="return validate()">
<p>
<input name="computer[]" type="checkbox" value="1">1
<input name="computer[]" type="checkbox" value="2">2
<input name="computer[]" type="checkbox" value="3">3
<input name="computer[]" type="checkbox" value="4">4<br>
<input type="submit" name="submit">
</form>

Jon Hanlon
06-04-2008, 09:11 PM
computer[] is not a valid name.

Try var computer2 = document.forms.classroom.input[0];

bsxiong
06-06-2008, 11:57 AM
i'm using computer[] becuase i'm treating it as an array for php so that i can insert it into my mysql DB

the computer[] is used in this PHP:


$computernumber=$_POST['computer'];
if ($computernumber)
{
$array = array();
foreach ($computernumber as $t)
{
array_push($array, " $t ");
}
$rooms = implode($array);
echo $rooms;

¥åßßå
06-06-2008, 02:12 PM
computer[] is not a valid name.

Really? You learn something new everyday!

You should have a unique ID for each of your checkboxes though, and an associated <label for="checkbox_ID"> for WAI/508 compliance, in which case you can getElementById( 'checkbox_'+## ) ;)

¥

Jon Hanlon
06-06-2008, 07:43 PM
Yes that's one of the problems with php.

You can use getElementById()
or
document.getElementsByName("computer[]");
or
document.forms.classroom.input[0];