dpojoxa5613 2019-04-23 03:03
浏览 101
已采纳

如何在提交后保持数组和动态复选框保持检查状态?

I have one dynamic checkbox (can be ++) that needs to stay checked after user submit.

I already tried some tricks like using a hidden input before my checkbox code in HTML. Now I stuck in doing an isset(_POST) and the checkbox didn't stay checked.

Here's my HTML:

<input type="hidden" name="hidden_name[]" id="hidden_name0">                                                                

<input type="checkbox" name ="name[]" id="name0">

<label for="name">Name</label>

--------UPDATE-------- And in my PHP file, here's the code:

$valueName = array();
        if(isset($_POST["hidden_name"]))
        {
                    foreach($_GET['hidden_name'] as $value)
                {
                    array_push($valueName,$value);
                }
        }

That code doesn't work :/

How to make the checkbox stays checked after user check it and submit the form? What should I write in PHP? Do I really need a hidden input before my checkbox?

  • 写回答

2条回答 默认 最新

  • douchen7366 2019-04-23 04:02
    关注

    isset might be returning false since the submitted value is NULL. I suggest adding a value='1' on the hidden input field -- or are you updating that field with the corresponding checkbox value?

    Alternatively, you have some other options:

    1. Change the name for each dynamic checkbox to have an identifier.

      <input type="checkbox" name="name-x" <?php echo isset( $_POST['name-x'] ) ? 'checked="checked"' : '' ?> />
      

      Where x could be your dynamic ID. Notice the addition of the PHP code and using a ternary operator, you can check the checkbox if the $_POST['name-x'] is set.

    2. Add a value to the checkbox.

      <input type="checkbox" name="name[]" id="name0" value="name0" />
      <input type="checkbox" name="name[]" id="name1" value="name1" />
      <input type="checkbox" name="name[]" id="name2" value="name2" />
      

      However, you need to match this value in your PHP code.

      <?php
      if ( isset( $_POST['name'] ) ) {
          $values = array();
          foreach( $_POST['name'] as $value ) {
              array_push( $values, $value );
          }
      }
      ?>
      

      Then you have to modify your checkbox again to have inline PHP.

      <input type="checkbox" name="name[]" id="name0" value="name0" <?php echo in_array( "name0", $values ) ? 'checked="checked"' : '' ?> />
      <input type="checkbox" name="name[]" id="name1" value="name1" <?php echo in_array( "name1", $values ) ? 'checked="checked"' : '' ?> />
      <input type="checkbox" name="name[]" id="name2" value="name2" <?php echo in_array( "name2", $values ) ? 'checked="checked"' : '' ?> />
      

    You can also create a function to display these inline codes to make it much cleaner. HTH!

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集