PHP/Javascript limiting amount of checkboxes
Posted
by Carl294
on Stack Overflow
See other posts from Stack Overflow
or by Carl294
Published on 2010-05-03T15:30:54Z
Indexed on
2010/05/03
15:48 UTC
Read the original article
Hit count: 255
Hi everyone
Im trying to limit the amount of checkboxes that can be checked, in this case only 3 can be checked. When using plain HTML this works fine. The code can be seen below.
HTML example
<td ><input type=checkbox name=ckb value=2 onclick='chkcontrol()';></td><td>Perl</td>
Javascript Function
<script type="text/javascript">
function chkcontrol(j) {
var total=0;
for(var i=0; i < document.form1.ckb.length; i++){
if(document.form1.ckb[i].checked){
total =total +1;}
if(total > 3){
alert("Please Select only three")
document.form1.ckb[j].checked = false;
return false;
}
}
}
</script>
The problem appears when replacing the fixed HTML values with values from a MYSQL database. All the information appears correctly, and can be posted to another page via a submit button. However, it seems like the 'value' assigned to each record from the database is not making its way too the javascript function.
<td><input name="checkbox[]" type="checkbox" value="<?php echo $rows['TCA_QID'];?>" onclick="chkcontrol();"></td>
I have tried changed the name in the javascript function to match the 'checkbox' name.Any advice would be greatly appreciated
Thanks
© Stack Overflow or respective owner