doupoji3856 2019-01-24 14:38 采纳率: 100%
浏览 56
已采纳

从数据库预先填充大量单选按钮组

I'm working on a project. I want to pre-populated data from database.

HTML

<div class="question">
  <input type="radio" name="q1" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q1" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q1" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
  <input type="radio" name="q2" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q2" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q2" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
  <input type="radio" name="q3" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q3" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q3" value="C" <?php echo $checkedC ?>>
</div>
<!-- etc till let's say 30 question -->

PHP

$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
for($i=1; $i<=30; $i++){
  switch($data[${"answer".$i}]{
   case "A" : $checkedA="checked"; break;
   case "B" : $checkedB="checked"; break;
   case "C" : $checkedC="checked"; break;
  }
}

Then, how to make the q1 which is corresponded with $data['answer1'] checked if it is filled with data from database, etc?

  • 写回答

2条回答 默认 最新

  • drj26159 2019-01-24 16:46
    关注

    Using the code you've provided, It's viable to get the data from the database and then show the result in your html and not over complicate things. To pre-populate your radio buttons, you need to have the following below:

    PHP

    $query="SELECT * FROM `quiz` WHERE email='$email'";
    $result=mysqli_query($link,$query);
    $data=mysqli_fetch_assoc($result);
    

    HTML

    <div class="question">
      <input type="radio" name="q1" value="A" <?php echo ($data['q1'] == 'A')? 'checked' : '';  ?>>
      <input type="radio" name="q1" value="B" <?php echo ($data['q1'] == 'B')? 'checked' : '';  ?>>>
      <input type="radio" name="q1" value="C" <?php echo ($data['q1'] == 'C')? 'checked' : '';  ?>>
    </div>
    <div class="question">
      <input type="radio" name="q2" value="A" <?php echo ($data['q2'] == 'A')? 'checked' : '';  ?>>
      <input type="radio" name="q2" value="B" <?php echo ($data['q2'] == 'B')? 'checked' : '';  ?>>
      <input type="radio" name="q2" value="C" <?php echo ($data['q2'] == 'C')? 'checked' : '';  ?>>
    </div>
    <div class="question">
      <input type="radio" name="q3" value="A" <?php echo ($data['q3'] == 'A')? 'checked' : '';  ?>>
      <input type="radio" name="q3" value="B" <?php echo ($data['q3'] == 'B')? 'checked' : '';  ?>>
      <input type="radio" name="q3" value="C" <?php echo ($data['q3'] == 'C')? 'checked' : '';  ?>>
    </div>
    

    With that piece of code, it sets checked depending on what the value coming from the database is and displays it on the screen.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题