douchun1961 2014-03-21 17:45
浏览 56
已采纳

验证后记住用户复选框

In my form, I have several checkboxes that are populated from a db table.

The issue is, when the user submits the form and gets error, the form can not remember his checked boxes!

here is the code I am working on:

while($n = mysqli_fetch_assoc($q))
{
echo '<div class="checkBoxesList"><label for="'.$n['eName'].'">'.$n['eName'].'</label><input type="checkbox" name="'.$n['eName'].'" id="'.$n['eName'].'" value="'.$n['id'].'" <?php echo (isset($_POST[\''.$n['eName'].'\'])?\'checked="checked"\':\'\') ?> /></div>';
}

I guess I have a problem in the 2nd echo within the main echo but I couldn't go over it.

FYI, I used to get this working by hard-coding all the checkboxes in the form e.g.

<div class="checkBoxesList"><label for="firstAid">First Aid</label><input type="checkbox" name="firstAid" id="firstAid" value="1"  <?php echo (isset($_POST['firstAid'])?'checked="checked"':'') ?> /></div>

Your help is highly appreciated.

  • 写回答

2条回答 默认 最新

  • dtq7387 2014-03-21 17:50
    关注

    Try like so:

    while($n = mysqli_fetch_assoc($q))
    {
     echo '<div class="checkBoxesList"><label for="'.$n['eName'].'">'.$n['eName'].'</label><input type="checkbox" name="'.$n['eName'].'" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST[$n['eName']]) ? 'checked="checked"' : '') .' /></div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)