duanchendu69495 2014-06-13 23:15
浏览 46
已采纳

Html表单提交复选框 - 在mysql表中更改值[关闭]

i have a database which is composed by some field that are booleans, so in my html form i made the input type set to check box and i want to make if checked = "True", uncheck = "false".
How can i archive that ? By now if i check them i see a 0, if i don't check i don't see nothing. I have tried to put and hidden field and work with that but not getting any results.

If you need code or print screen to understand the question better please tell me. Thank you.

  • 写回答

2条回答 默认 最新

  • doushi1510 2014-06-13 23:25
    关注

    That's the default behavior of checkbox inputs:

    • Post the value of the parameter "value" if the checkbox is checked
    • Post nothing if the checkbox isn't checked

    so, if you have something like:

    <input type="checkbox" value="TRUE" name="my_checkbox" />
    

    In PHP, you could do something like:

    if (isset($_POST["my_checkbox"])) {
        echo "checkbox is checked";    
    } else {  
        echo "checkbox not checked";
    }
    

    If you want absolutely to post something even if the checkbox isn't checked, as you said, you ca use an hidden field like this:

    <input type="checkbox" value="TRUE" name="my_checkbox_1" />
    <input type="hidden" value="my_checkbox_1,my_checkbox_2,my_checkbox_3" name="my_checkboxes" />
    

    the hidden field "my_checkboxes" would hold all the name of all the check boxes in your form.

    Then, you can take the value of $_POST["my_checkboxes"], split the value of it (with ","), and loop through all the checkboxes:

    $checkboxes = explode(",", $_POST['my_checkboxes']);
    foreach($checkboxes as $checkbox) {
      $checkbox_value = $_POST[$checkbox];
      if (isset($checkbox_value)) {
        echo "checkbox " . $checkbox . " was checked";
      } else {
       echo "checkbox " . $checkbox . "wasn't check";
      }
    }
    

    That solution would be good if you have a dynamic form with an unknown number of checkboxes.

    Posting something as "my_checkbox" ($_POST['my_checkbox']) if it is checked or not with different value would be possible with JavaScript (but I don't recommend it).

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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用