简体   繁体   English

表单提交后如何保持复选框被选中

[英]How to keep check-boxes checked after form submission

I have 3 check-boxes where I can submit them into MySQL.我有 3 个复选框,可以将它们提交到 MySQL。 It inserts perfectly, but the problem is, is when I go back to to edit the game, the check-boxes are not checked at all with their correct values (they are empty).它完美插入,但问题是,当我返回编辑游戏时,复选框根本没有使用正确的值进行检查(它们是空的)。 I know there are a lot of answers on this website about this problem, but I found none of them to work.我知道这个网站上有很多关于这个问题的答案,但我发现它们都不起作用。 Maybe its something that Im doing wrong with the code.也许是我在代码上做错了。 I will post it here.我会把它贴在这里。

THIS IS THE SHORT VERSION OF MY CODE, BECAUSE ITS TOO BIG.这是我的代码的简短版本,因为它太大了。

if (isset($_GET['add']) || isset($_GET['edit'])) {

  $checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? sanitize($_POST['available_consoles']) : '');

 if (isset($_GET['edit'])) {
    $checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? $_POST['available_consoles'] :  $game['available_consoles']);
 }

 if ($_POST) {
    // Separate ech checkbox value with a SPACE into the database.
    $checkbox = implode(', ', $_POST['available_consoles']);

    // DO INSERT HERE
 }

}
<!-- Add the Add Form -->
        <form action="games.php?<?php echo ((isset($_GET['edit'])) ? 'edit='.$edit_id : 'add=1'); ?>" method="post" enctype="multipart/form-data">

          <!-------------- AVAILABLE CONSOLES ------------------->
            <div class="form-group col-md-6">
                <label for="checkbox">Available Consoles:&nbsp;</label>
                <label class="checkbox">
                    <input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="Xbox One">
                </label>
                <label class="checkbox">
                    <input type="checkbox" <?php if (in_array('PS4', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PS4">
                </label>
                <label class="checkbox">
                    <input type="checkbox" <?php if (in_array('PC', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PC">
                </label>
            </div>
           <div class="form-group pull-right">
                <a href="games.php" class="btn btn-default">Cancel</a>
                <input type="submit" value="<?php echo ((isset($_GET['edit'])) ? 'Edit ' : 'Add '); ?> Game" class="btn btn-success">
            </div>

</form>

Getting these error message next to checkbox:在复选框旁边收到这些错误消息:

Notice: Undefined index: available_consoles in C:\xampp\htdocs\Gamesite\admin\games.php on line 425

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\Gamesite\admin\games.php on line 425
name="available_consoles[]" value="Xbox One">

Getting it for all 3 checkboxes获取所有 3 个复选框

this is what you need这就是你所需要的

<input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?>id="checkbox" name="available_consoles[]" value="Xbox One"> 

replace in_array('Xbox One' with the other consoles names for each inputin_array('Xbox One'替换为每个输入的其他控制台名称

Here is simple function:这是简单的功能:

function checked($t,$v){
  if (in_array("{$t}", $v)) {
    return 'checked';
  }
}

use : <?php echo checked('Xbox One',$con); ?>使用: <?php echo checked('Xbox One',$con); ?> <?php echo checked('Xbox One',$con); ?>

it was not an array nor was it from $_POST it was actually from the DB so i appended my code to match the answer.它不是数组,也不是来自 $_POST 它实际上来自数据库,所以我附加了我的代码以匹配答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM