gpt4 book ai didi

php - Checkbox null表示不是,checkbox checked是1

转载 作者:行者123 更新时间:2023-11-29 03:31:46 27 4
gpt4 key购买 nike

我想拥有它,所以如果复选框为 NULL 或 0,则它不像值为 1 时那样喜欢。另外,有没有办法将选中的框限制为 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>

最佳答案

要检查它是 NULL 还是 0 否则 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>

关于php - Checkbox null表示不是,checkbox checked是1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com