1

I have an array like this

$current_asset = [
    ['name'=>'Land,'id'=>1],
    ['name'=>'Building ,'id'=>2],
    ['name'=>'Machinery','id'=>3],
];

<?php 
 foreach($current_asset as $key=>$value){ ?>
 <input type="checkbox" name="current_asset[]" value="<?php echo $value['id'] ?>">

 <?php } ?>

My question how can I add checked attribute in if one of the value is checked when the form populated with the POST data

I am getting the checkbox array like this on form submit

Here are the current checkboxes on form submit (ie values of current_asset)

Array(
    0=>1
    1=>1
   )
7
  • What output do you want? Commented May 26, 2018 at 17:13
  • I presume that you want to have the checkbox checked when you print out the <input >, but what defines if the value is supposed to be checked? Commented May 26, 2018 at 17:17
  • Or is it a question of how to know which array element was chosen after submission? In which case, I would suggest making the array to be an associative array where the index is the id number. Commented May 26, 2018 at 17:21
  • @Andreas when the form is populated with old data i want to add the checked attributte to the checkbox Commented May 26, 2018 at 17:34
  • @TimMorton can you give an example?? Commented May 26, 2018 at 17:37

2 Answers 2

1

You would need to do some kind of check before printing

$html=‘’;
foreach($current_asset as $asset) {
 if($asset[‘hasBeenCheckedBefore’]) {
  $checked = ‘checked’;
 } else {
  $checked = ‘’;
 }
 $html .= “$asset[‘name’] <input type=‘checkbox’ name=‘current_asset[]’ value=‘$asset[“id”]’ $checked />”;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yup, but after your foreach and before hte if you'll want to set $checked=''; or similar, so that 1 - no undefined var warning/error, and 2- after the first checked value is found it is reset for the next trip through the loop.
True! Edited my post
0

Here is an example of one way to do it. I have changed your data structure to be easier to use. I was confused initially because you didn't mention any way to store data. So this is only good for the one page view.

<?php

// initialize data

/**
 * Data structure can make the job easy or hard...
 *
 * This is doable with array_search() and array_column(),
 * but your indexes might get all wonky.
 */
$current_asset = [
    ['name'=>'Land','id'=>1],
    ['name'=>'Building' ,'id'=>2],
    ['name'=>'Machinery','id'=>3],
];

/**
 * Could use the key as the ID.  Note that it is being
 * assigned as a string to make it associative.
 */
$current_asset = [
    '1'=>'Land',
    '2'=>'Building',
    '3'=>'Machinery',
];

/**
 * If you have more information, you could include it
 * as an array.  I am using this setup.
 */
$current_asset = [
    '1'=> ['name' => 'Land',      'checked'=>false],
    '2'=> ['name' => 'Building',  'checked'=>false],
    '3'=> ['name' => 'Machinery', 'checked'=>false],
];


// test for post submission, set checked as appropriate
if(array_key_exists('current_asset', $_POST)) {
  foreach($_POST['current_asset'] as $key => $value) {
    if(array_key_exists($key,$current_asset)) {
      $current_asset[$key]['checked'] = true;
    }
  }
}



// begin HTML output
?>
<html>
  <head>
    <title></title>
  </head>
  <body>

    <!-- content .... -->

    <form method="post">
    <?php foreach($current_asset as $key=>$value): ?>
      <?php $checked = $value['checked'] ? ' checked' : ''; ?>
      <label>
        <input type="checkbox" name="current_asset[<?= $key ?>]" value="<?= $key ?>"<?= $checked ?>>
        <?= htmlentities($value['name']) ?>
      </label>
    <?php endforeach; ?>
    </form>

  <body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.