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条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan