samedi 5 décembre 2015

Submit multiple checkboxes using POST with php while using Javascript to limit checked items

So. Basically I know now how to add a limit to selected check boxes. I also found out how to send multiple values via POST on the checkboxes by adding '[]' to their names. Another problem arises. How do you combine those two? What scriptcode should I use? because the code I used below does not accept checkboxes with '[]'. Here's my code:

<script type="text/javascript">
function checkboxlimit(checkgroup, limit){
    var checkgroup=checkgroup
    var limit=limit
for (var i=0; i<checkgroup.length; i++){
    checkgroup[i].onclick=function(){
    var checkedcount=0
    for (var i=0; i<checkgroup.length; i++)
        checkedcount+=(checkgroup[i].checked)? 1 : 0
    if (checkedcount>limit){
        alert("You can only select a maximum of "+limit+" candidates.")
        this.checked=false
        }
    }
    }
}

</script>

<body>
<form action="votePage.php" method="post" enctype="multipart/form-data" id="voting" name="voting">
<?php 
$limit = 0;
while($limit<3){
?>
<input type='checkbox' name='Candidate' value=<?php echo $limit;?>><?php echo $limit;?> <br>
<?php
$limit++;
}

?>
<input type="submit" name="submit"/>
</form> 
<script type="text/javascript">

checkboxlimit(document.forms.voting.Candidate, 2) //this limits checked items to only 2.
</script>
</body>

A quick helper will be greatly appreciated. Thnx.

Aucun commentaire:

Enregistrer un commentaire