<分区>
<分区>
想法是让用户选择流派类型的复选框列表。流派输入来自数据库。因为sql会返回一个数组的结果,一个for循环得到所有的值,并把它们做成一个check box。错误来自循环中的第三行。它显示 undefined index 类型
<div class="list-group">
<h3>Genre</h3>
<?php
$search_genre = "SELECT DISTINCT(genre) FROM movie_db WHERE product_status ='1' ORDER BY movieid DESC"
$statement = mysqli_query($conn,$search_genre);
$result = mysqli_fetch_all($statement);
foreach ($result as $row){
?>
<div class="list-group-item checkbox">
<label><input type="checkbox" class="common_selector genre" value="<?php echo$row['genre'];?> > <?php echo $row['genre'];?></label>
</div>
<?php
}
?>
最佳答案
您需要为复选框命名,并且由于您允许多项选择(复选框与单选按钮),因此您需要使用 PHP 数组样式名称 -
<label>
<input
name="mycheckbox[]"
type="checkbox"
class="common_selector genre"
value="<?php echo $row['genre'];?>"
<?php // here is where you could throw a check to make it pre-selected
by printing the word "checked" if a logical condition is met, etc ?>
>
<?php echo $row['genre'];?>
</label>
现在,在您的处理脚本中,您可以引用 $_POST['mycheckbox']
,它将包含一个包含一个或多个值的数组,每个选定值一个元素。
for($i=0;$i<count($_POST['mycheckbox']);$i++){
print("selected value $i: ".$_POST[['mycheckbox'][$i]."\n");
}
编辑-
只是为了开玩笑,如果你只想要一个单一的选择(单选按钮),你仍然将元素命名为相同的东西,你只需省略 PHP 数组括号 []
<label>
<input
name="myradio"
type="radio"
class="common_selector genre"
value="<?php echo $row['genre'];?>"
<?php /* here is where you could throw a
check to make it pre-selected
by printing the word "selected"
if a logical condition is met, etc
note in a series the last one selected
stays selected if you print selected more than once
*/
?>
>
<?php echo $row['genre'];?>
</label>
并且选定的 radio 组值可用作 $_POST['myradio']
关于php - 未定义索引 : PHP checkbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52911241/
相关文章:
php - 如何更改 OSX 上的 docker 安装卷的权限以与运行 Laravel 5.1 的 Nginx Web 服务器一起使用?
javascript - jQuery/css让两个div显示在同一行
javascript - 让 Paypal 捐赠改变 CSS
php - 使用计数器更新 codeigniter 事件记录
PHP file_get_contents 和 if 返回 false
javascript - 具有多列的 CSS 列表,具有水平滚动和自动列宽和高度