我想动态填充复选框,我从中获取数据库中的值并存储在复选框中,但不显示值如何显示值。
这是我的代码:
<div>
<?
//echo $eventid=$_POST['events'];
$count=count($_POST['events']);
for($i=0; $i<$count; $i++){
$select="select b.first_name,b.last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
$res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
if ($res->num_rows > 0)
{
while($row = $res->fetch_assoc ())
{
?>
<input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />
<?
}
}
}
?>
</div>
答案 0 :(得分:1)
复选框的值属性应为
value="<?php echo $row['first_name']." ".$row['last_name']?>"
试试这个
<input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name']." ".$row['last_name']?>"/><br />
而不是
<input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />
更新2:
<input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name']." ".$row['last_name']?>"/>
<?php echo $row['first_name']." ".$row['last_name']?> <br />
答案 1 :(得分:0)
从评论开始(您要将名字设置为值):
$select="select b.first_name,b.last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
更改
<input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />
到
<input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name'];?>"/><br />
并且(因为您从两个表中选择),您应该在查询中使用AS,如:
$select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
(可能没必要)
另请查看prepared statements以防止SQL注入。
答案 2 :(得分:0)
试试这个:
<div>
<?php /* <-- Don't use PHP short tags, it's a bad practice, they are deprecated */
/* Making select statement inside a loop is a very bad idea.
Also, you should make sure to properly escape
$_POST['events']), as inserting unescaped inputs into a query
is very unsafe */
$post = array_map(
array($GLOBALS['mysqli'], "escape_string"),
$_POST['events']
);
/* We apply mysqli::escape_string to every element of
$_POST['events'] and save result as $post */
$select = "SELECT `b`.`first_name`, `b`.`last_name` FROM `buyers` `b` ".
"JOIN `registrations`.`r` ON `r`.`buyer_id` = `b`.`buyer_id` ".
"WHERE `r`.`event_id` IN ('".implode("','", $post)."') ".
"GROUP BY `r`.`buyer_id`";
$res = $GLOBALS ['mysqli']->query ($select)
or die ($GLOBALS ['mysqli']->error . __LINE__);
while($row = $res->fetch_assoc()) : ?>
<label>
<input type="checkbox" name="receptionts[]" checked="checked" value="<?= $row['first_name']." ".$row['last_name']?>"/>
<?= $row['first_name']." ".$row['last_name'] ?>
</label>
<?php endwhile; ?>
</div>
答案 3 :(得分:-1)
像这样给予价值
<input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name'].' '.$row['last_name'];?>"/><br />