【问题标题】:Show true or false (0/1) from a mySQL in checkbox在复选框中显示来自 mySQL 的 true 或 false (0/1)
【发布时间】:2017-12-05 05:41:18
【问题描述】:
<input type="text" id="A1" name="A1">

在上面的框中,0 或 1 是从 mysql 中放置的(fetch in box)。现在我想要那个:

value box = 0 >> 复选框未选中

value box = 1 >> 复选框被选中

如何使用复选框(type="checkbox")而不是文本(type="text")?

<input type="checkbox" id="A1" name="A1">

【问题讨论】:

  • 数据是如何从 MySQL 中插入的?有一个简单的回声还是什么?
  • 你能解释一下你的问题吗@Reza Hatami
  • 您想从数据库中获取值(真/假)并显示在您的 html 复选框中吗?
  • @adeneo 带触发器 mysql

标签: javascript php jquery mysql checkbox


【解决方案1】:

你可以这样检查:

<?php
$box_val = 1; // rertive this value from database
?>
<input type="checkbox" id="A1" name="A1" <?=($box_val == 1) ? "checked" : "" ?>>

【讨论】:

  • 提交表单后如何根据checkbox状态向数据库发送0或1?
  • 设置复选框的值。只有选中复选框,您才会获得复选框值。
【解决方案2】:

你可以这样做:

<input id="A1" name="A1" checked="0" type="checkbox">
<script>
    $('#A1').prop("checked",$mySqlValue);
</script>

如果$mySqlValue==1 将选中该框,如果$mySqlValue==0 将不选中该框。

【讨论】:

    【解决方案3】:

    你可以用js

    document.getElementById("A1").checked = true;
    

    【讨论】:

      【解决方案4】:
      <input type="checkbox"  id="A1" name="A1"   <?php if($value==1) echo 'checked="checked"'; ?> >
      

      $value 来自数据库

      【讨论】:

        【解决方案5】:

        你好礼萨哈塔米,

        试试下面的代码,
        $my_checkbox 存储数据中的变量从 mysql 数据库中检索。

        <?php
            $my_checkbox = 1;
        ?>
        <input type="checkbox" id="Notification" name="Notification" <?php echo if($mycheckbox == 1) ? 'checked=""': ""; ?> />
        

        也在使用,但是在这个问题是某些浏览器或php版本不支持所以我建议使用上面的代码。

        <?php
           $my_checkbox = 1;
        ?>
            <input type="checkbox" id="Notification" name="Notification" <?=($my_checkbox == 1) ? "checked" : "" ?> />
        

        希望我的回答对你有所帮助。如有任何疑问,请发表评论。

        【讨论】:

          猜你喜欢
          • 2012-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-17
          • 1970-01-01
          • 2015-03-12
          • 2014-06-03
          相关资源
          最近更新 更多