我想拥有它,如果一个复选框是NULL或0,它就不像它是一个值,如果值是一个。 还有,有办法将选中的方框限制为10?
当前代码:
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST'favorites')){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['favorites[]'] as $selected){
$query= "UPDATE u_collection SET fav = 0 WHERE username ='".$username."'";
mysqli_query($db,$query);
}
}
}
foreach($gems as $i){
echo '<tr>
<th>'.$i->name.'</th>
<th><form action="collection.php" method="post">
<input type="checkbox"';
if($i -> fav){
echo 'checked = "checked"';
}
echo 'name="favorites[]" value='.$i->name.'></th></tr></form>';
}
?>
<th>
<input type='submit' name='submit' value='Submit'/>
</form>
答案 0 :(得分:0)
检查它是否为NULL或者否则为1我将使用empty()函数
foreach($gems as $i){
echo '<tr>
<th>'.$i->name.'</th>
<th><form action="collection.php" method="post">
<input type="checkbox"';
if(!empty($i->fav)){
echo ' checked = "checked" ';
}
echo 'name="favorites[]" value='.$i->name.'></th></tr></form>';
}
要将检查的框限制为10,我将使用javascript(jQuery),如下所示:
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$('form input[type=checkbox]').on('click', function(){
if ($('form input[type=checkbox]:checked').length > 10)
{
$(this).removeAttr('checked');
alert('You are only allowed to select 10 checkboxes');
}
});
</script>