dswmmvrg40957 2019-01-22 11:24
浏览 57

当值与数据库中的值匹配时,如何显示单选按钮

I have a list where people can answer questions, each question has three possible answers using radio buttons.

Inserting that data in my database works fine but now I want to be able to edit those lists so I need to display an already filled in list from my database instead of inserting a new one.

Normal data is no problem but I am having some issues with the radio buttons.

My database structure looks like this:

wpi_info
-id
-other non important fields for this question 

wpi_categories 
- id
- title
- info_id (same as id of wpi_info)

wpi_questions
-id 
-title
-answer
-cid (same as id of wpi_categories)

This is how I currently show the radio buttons (categories/questions show up but the radio buttons are not checked):

<?PHP
$een = 1;
$twee = 2;
$drie = 3;
$getcats = 'SELECT * FROM wpi_categories WHERE info_id = "'.$conn->real_escape_string($getinfo['id']).'" ORDER BY id';
$getcatscon = $conn->query($getcats);
while($getcats = $getcatscon->fetch_assoc()){
    if(!empty($getcats['title'])){
      $werkplekinspectie .= '
      <label class="categorytitle">'.$getcats['title'].'</label>
      <div class="row">';

      $getquestions = 'SELECT * from wpi_questions WHERE cid = "'.$getcats['id'].'"';
      $getquestionscon = $conn->query($getquestions);
      while($getquestions = $getquestionscon->fetch_assoc()){
        $werkplekinspectie .= '
        <div class="col-md-8">
          <p class="questionclass">'.$getquestions['title'].'</p>
        </div>
        <div class="col-md-4">
          <div class="container text-right">
              <input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$een.'" value="ok" required>
              <label class="radiotoggle" for="radio-'.$een.'"><span class="radio">Ok</span></label>
              <input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$twee.'" value="fout">
              <label class="radiotoggle" for="radio-'.$twee.'"><span class="radio">Fout</span></label>
              <input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$drie.'" value="nvt">
              <label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
            </div>
        </div>';
        $een+=3;
        $twee+=3;
        $drie+=3;
      }
      $werkplekinspectie .= '
      </div>';
    }
}
echo $werkplekinspectie;
?>

The values ok, nvt, fout are what is stored in the answer column of wpi_questions.

For every radio button, if the answer matches the value I need it to be checked. How can I do that?

  • 写回答

3条回答 默认 最新

  • doudi1979 2019-01-22 11:28
    关注

    You can do it in jquery by checking the value from database and keeping it as checked by

     $('#id').prop('checked', true);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝