【问题标题】:PHP and HTML remember checkbox value with same name after submitPHP和HTML在提交后记住具有相同名称的复选框值
【发布时间】:2014-03-26 16:19:25
【问题描述】:

我在表单中有一些同名的字段,它们是从数据库表中自动生成的,我不知道会有多少。

   <div class="row w40 fl">
    <label for="code" class="label">codice <span class="form-notice">*</span></label>
    <input type="text" class="input" name="code" id="code" placeholder="Codice" value="<?= Input::post('code') ?>">
</div>

<div class="row w40 fr">
    <label for="name" class="label">nome <span class="form-notice">*</span></label>
    <input type="text" class="input" name="name" id="name" placeholder="Nome" value="<?= Input::post('name') ?>">
</div>

<?php foreach($categories as $cat): ?>
    <div class="w20 fl">
        <label for=""><?= ucwords($cat->name) ?></label>
        <input type="checkbox" name="cat[]" value="<?= $cat->id ?>" <?= ($cat->id == Input::post('cat')) ? 'checked' : '' ?>>
    </div>
<?php endforeach; ?>

问题是如果提交后出现错误,所有勾选的输入都会丢失。如果它们有不同的名称,那不会有任何问题,但它们总是不同的。

那么在提交之后,我如何才能记住选中的并检查它们呢?假设用户检查了其中的 5 个,填写了所有表单输入但留下一个空白。显然会弹出一个错误,但是虽然所有其他输入都会有用户输入的数据,但复选框不会,至少我不知道该怎么做。

注意Input::post('code') 表示(isset($_POST['code'])) ? $_POST['code'] : ''

谢谢你,希望你能给我一个解决方案。

好的,经过更多研究,这里是解决方案。

if ( isset($_POST['cat']) ) {
    foreach ($_POST['cat'] as $cbCat) {
        $selectedCat[$cbCat] = 'checked';
    }
}

<?php foreach($categories as $cat): ?>
    <div class="w20 fl">
        <label for="cat-<?= $cat->id ?>"><?= ucwords($cat->name) ?></label>
        <input type="checkbox" id="cat-<?= $cat->id ?>" name="cat[]" value="<?= $cat->id ?>" <?= (isset($selectedCat[$cat->id])) ? $selectedCat[$cat->id] : '' ?> >
    </div>
<?php endforeach; ?>

【问题讨论】:

    标签: php html post checkbox form-submit


    【解决方案1】:

    可能的解决方案: cat[] 是一个数组,所以你可以给它数字索引来识别哪个复选框是哪个:

    <?php $i=1;
    foreach($categories as $cat): ?>
        <div class="w20 fl">
            <label for=""><?= ucwords($cat->name) ?></label>
            <input type="checkbox" name="cat[<?php echo $i ?>]" value="<?= $cat->id ?>" <?= ($cat->id == Input::post('cat[$i]')) ? 'checked' : '' ?>>
        </div>
    <?php i++ ;?>
    <?php endforeach; ?>
    

    我只是不确定你是否可以将这样的变量 Input::post('cat[$i]')) 传递给你的方法

    【讨论】:

    • 我试过了,我确实不能。我最终找到了解决方案,如果有兴趣,请参阅主帖。
    • 如果您不介意改变您的方法,那么还有更简单的解决方案。就在您的第一个代码更改中:($cat-&gt;id == Input::post('cat')) 到:(in_array($cat-&gt;id, $_POST['cat']))
    • 谢谢,很好的解决方案。
    【解决方案2】:

    这里有一些代码显示了复选框会发生什么。

    关于它们的重要一点是,未检查的不会在 $_POST 中发送值!只有 CHECKED 可以。我提供了一些代码来说明这一点。

    $_POST 的 'var_dump' 允许您查看从“复选框”表单中真正返回的内容。

    这是经过测试的代码。

    它适用于 windows XP 上的 PHP 5.3.18。

    <?php
    
    // we will have five checkboxes and each will have a separate value as follows...
    $checkboxValues = array('cbx01', 'cbx02', 'cbx03', 'cbx04', 'cbx05');
    
    // This array keyed on the checkbox VALUE will be checked or not...
    // This is ALL the checkboxes!
    $checkboxIsChecked = array('cbx01' => false,
                               'cbx02' => false,
                               'cbx03' => false,
                               'cbx04' => false,
                               'cbx05' => false);
    
    /*
     * Determine the 'cat' checkboxes that are actually currently checked!
     *
     * We can know this because the $_POST['cat'] array
     * will contain VALUES for the Checkboxes that the user ACTUALLY checked!
     *
     * The UNCHECKED Checkboxes do NOT send any VALUE so they are MISSING from the array!
     *
     * i.e. if checkbox value: 'cbx02' and  'cbx04' are checked by the user then
     * the 'cat' array will be :
     *   0 => 'cbx02'
     *   1 => 'cbx04'
     *
     * All the other checkboxes will be 'unchecked'.
     *
     *
     */
    var_dump($_POST); // you can see what comes in!
    
    if (isset($_POST['cat'])) { // may have some checked 'cat' checkboxes...
    
      foreach ($_POST['cat'] as $checkboxValue) {
        // mark the appropriate checkbox as 'CHECKED' i.e cbx03
        $checkboxIsChecked[$checkboxValue] = true;
      }
    }
    
    ?>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Show Checkboxes and the input from them.</title>
      </head>
    
      <body>
        <div class="main" id="main">
    
            <!-- heading -->
            <strong><?php echo 'Test checkbox setting...'?></strong><br/>
    
            <form method="POST" action="">
    
            <?php foreach($checkboxValues as $cbxValue): ?>
              <div>
                <?php $labelId = $cbxValue . '_id'; // generate a label ?>
                <label for="<?php echo $labelId?>"><?= ucwords('A '. $cbxValue . ' thingy!') ?></label>
                <input type="checkbox"
                        name="cat[]"
                        id="<?php echo $labelId?>"
                        value="<?php echo $cbxValue?>"
                        <?php echo $checkboxIsChecked[$cbxValue] ? 'checked' : '' ?>>
              </div>
              <?php endforeach; ?>
    
              <input type="submit" value="GO"/>
            </form>
        </div>
      </body>
    </html>
    

    【讨论】:

    • 感谢您的回复。你很好地解释了复选框是如何工作的,但在我看来,对于我想要实现的目标来说,代码太多了,有更多的可能性来做我想做的事情,我选择了较短的那个。您如何看待@MarkS 解决方案?我觉得它非常好,而且更干净。
    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多