I am trying to check whether the checkbox is checked or not using jquery when the checkbox inside the php foreach loop.
i am trying to pass the checkbox value using php foreach loop values are passed correctly but cehckbox validations are not working.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Checkbox Reference</title>
<script type="text/javascript" src="http://ift.tt/wV40hO"> </script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform").submit(function(){
valid = true;
if($('input[type=checkbox]:checked').length == 0)
{
alert ( "ERROR! Please select at least one checkbox" );
valid = false;
}
return valid;
});
});
</script>
</head>
<body>
<form name="myform" id="myform" action="" method="post" onSubmit="return validate();">
<?php
$values = array(1,2,3,4,5);
foreach($values as $id)
{
echo "<input type='checkbox' name='checks[]' value='".$id."'><br>";
}
?>
<input type="submit" name="submit" value="submit">
</form>
<?php
?>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire