【问题标题】:How can I show a radio button as being checked when the value matches a value in my database当值与数据库中的值匹配时,如何将单选按钮显示为已选中
【发布时间】:2019-06-15 20:40:35
【问题描述】:

我有一个人们可以回答问题的列表,每个问题都有三个使用单选按钮的可能答案。

在我的数据库中插入该数据可以正常工作,但现在我希望能够编辑这些列表,因此我需要从我的数据库中显示一个已填写的列表,而不是插入一个新列表。

正常数据没问题,但单选按钮有一些问题。

我的数据库结构如下:

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)

这是我目前显示单选按钮的方式(显示类别/问题,但未选中单选按钮):

<?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;
?>

oknvtfout 的值存储在wpi_questionsanswer 列中。

对于每个单选按钮,如果答案与我需要检查的值匹配。我该怎么做?

【问题讨论】:

  • values="whatever"> 你试过了吗?

标签: php loops radio


【解决方案1】:

你可以在 jquery 中通过检查数据库中的值并保持它被检查的方式来做到这一点

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

【讨论】:

  • 请查看OP使用的标签。
【解决方案2】:

在此处输入代码,使用以下代码 sn-p 作为示例。我已将男性设置为默认值,您可以将女性放入性别变量中

<?php $gender ='male';?>
    <input type="radio" name="gender" value="male" <?php echo isset($gender) && !empty($gender) && $gender == 'male' ? 'checked'  :'' ; ?> > Male<br>
    <input type="radio" name="gender" value="female" <?php echo isset($gender) && !empty($gender) && $gender == 'female' ? 'checked'  :'' ; ?> > Female<br>

【讨论】:

    【解决方案3】:

    您可以简单地检查答案是否匹配并输出checked 属性:

    '...
    <input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$een.'" value="ok" required ' . ($getquestions['answer'] == 'ok' ?'checked':'') . '>
    <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" ' . ($getquestions['answer'] == 'fout'?'checked':'') . '>
    <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" ' . ($getquestions['answer'] == 'nvt'?'checked':'') . '>
    <label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
    ...'
    

    但是整个代码块很难阅读,随着时间的推移肯定会产生错误。

    人们普遍认为,将数据访问逻辑与 html 混合是一个糟糕的想法,但如果您被困在维护一个巨大的旧旧应用程序中,那么完全重构可能不可行

    在这种情况下,您至少可以通过将数据访问置于顶部,将 html 生成置于下方,使用 phps 模板语法而不是巨大的字符串连接,从而使您的代码更易于维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2013-12-26
      相关资源
      最近更新 更多