【问题标题】:How do I make a radio button selected by reading the value from my database?如何通过从数据库中读取值来选择单选按钮?
【发布时间】:2014-04-04 01:11:27
【问题描述】:

我有一个带有一些单选按钮、一个文本字段和一个文本区域的表单,我将它们插入到我的数据库中。现在我可以选择编辑整个内容。当我单击按钮编辑时,我再次获得表单,文本字段和文本区域中的值与我插入数据库中的值相同。现在我不知道如何获得具有正确值的单选按钮作为检查的数据库中的值。

这是我的表格:

<<<EOT
            <table>
  <form action="index.php?page=artikelen_bewerken_verwijderen&id={$_GET['id']}" method="post"> 
  <tr><th><td><input type="text" size="108%" name="title" value="{$titel}"></td></tr></th> 
  <tr><th><td> <textarea name="description"  rows="20" cols="80" >{$nieuwsbericht}</textarea></td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="borussia-dortmund">Borussia Dortmund </td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="ajax">Ajax</td></tr></th>
  <tr><th><td><input type="radio" name="club" value="ac-milan">AC Milan</td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="fc-bayern">Bayern Munchen</td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="feyenoord">Feyenoord</td></tr></th>
  <tr><th><td><input type="radio" name="club" value="juventus">Juventus</td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="real-madrid">Real Madrid</td></tr></th>
  <tr><th><td><input type="radio" name="club"  value="fc-barcelona">Barcelona</td></tr></th>   
  <tr><th><td><input name="edit" type="submit" value="Edit"></td></tr></th> 
  </form> 
 </table>
EOT;

有人知道如何像我想要的那样检查单选按钮吗?

【问题讨论】:

  • @Whitebird 谢谢。没找到!
  • 这些事情发生了!这就是为什么有一个评论系统:)。
  • @Whitebird 仍然无法正常工作。
  • @Whitebird 没有错误,但我正在使用

标签: php html mysql radio-button


【解决方案1】:

最好的办法是把整个事情放到一个 json 数组中,然后让 javascript 来做。

如果只能选择一个单选按钮:

var radio_checked = '<?php echo $row['club']; ?>';

$(document).ready(function() { // start this when site is fully loaded
    if (radio_checked) { // check if variable is empty or not
        $(':radio[value='+radio_checked+']').prop('checked', true);
    }
});

或者如果选中了多个单选按钮(我假设您将结果转换为 PHP 中的数组):

var radio_array = <?php echo json_encode($yourArray); ?>;   

$(document).ready(function() { // start this when site is fully loaded
    if(!$.isArray(radio_array) || !radio_array.length) { // check if array is empty or not
        for (index in radio_array) {
            $(':radio[value='+radio_array[index]+']').prop('checked', true);
        }
    }
});

如果不想用jQuery,有纯PHP的方式,不过这每次都需要编码:

<input type="radio" name="club" <?php echo ($row['club'] == 'borussia-dortmund')? 'checked' : ''; ?> value="borussia-dortmund">
<input type="radio" name="club" <?php echo ($row['club'] == 'ajax')? 'checked' : ''; ?> value="ajax">

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    从您的数据库中获取值并与单选按钮的值进行交叉检查。如果它们匹配,请为您的收音机添加一个“已检查”属性。

    <input type="radio" name="club" value="borussia-dortmund" <?= ($valueFromDatabase == 'borussia-dortmund') ? 'checked' : ''?>>
    

    【讨论】:

      【解决方案3】:

      假设您的 mysql 结果数组是:$row,假设您的单选按钮索引是:radio,现在 $row['radio'] 这是您从数据库读取的单选按钮,将条件设置为选中或未选中,如下所示

      <input type="radio" <?php if($row['radio']==1){?> 'checked' <?php } ?>/>
      
      Let me know if you want more..
      

      【讨论】:

      • 最好提供一个确切的代码 sn-p 以查看它的运行情况。
      猜你喜欢
      • 2015-01-28
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2013-02-23
      • 2013-07-30
      • 1970-01-01
      • 2015-01-18
      • 2016-02-12
      相关资源
      最近更新 更多