Question

I have some some fields in a form that share the same name, they are automatically generated from a database table and I can't know how many would there be.

   <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; ?>

The problem is that if a error occurs after the submit, all the checked inputs will be lost. If they would have different names there wouldn't be any problem, but they always different.

So after the submit, how can I remember the checked ones and check them ? Say the user check 5 of them, fills all the form inputs but leaves one blank. Obviously an error will pop out, but while all other inputs will have the data the user entered, the checkboxes won't, at least I don't know how to do it.

Note that Input::post('code') means (isset($_POST['code'])) ? $_POST['code'] : ''

Thank you, hope you can suggest me a solution.

Ok, so after more research here is the solution.

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; ?>

No correct solution

OTHER TIPS

Possible solution: cat[] is an array, so you can give it numeric indexes to recognize which checkbox is which:

<?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; ?>

I'm just not sure if you can pass variable like this Input::post('cat[$i]')) to your moethod

Here is some code that shows what happens with checkboxes.

The important point about them is that UNCHECKED ones DO NOT SEND A VALUE in $_POST! Only CHECKED ones do. I supply some code that demonstrates the point.

The 'var_dump' of $_POST allows you to see what really comes back from the form for 'checkboxes'.

It is tested code.

It works on PHP 5.3.18 on windowx XP.

<?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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top