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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了