0

How can I include both selected and unselected boxes along with the checked and unchecked respective attributes?

$input_item = "".((isset($_POST["item"]))?implode(", ",$_POST["item"]):"")."";
$selectedCheckboxes = explode(', ', $input_item);

while($row = mysqli_fetch_array($result)) {

 echo "<p>
      <input type='checkbox' name='item[]' value='".$row['item']."' id='item_0'".(in_array($row['item'], $selectedCheckboxes)?'checked':'')."> ".$row['item']."</p>
    ";

   
}
Shun Zu
  • 119
  • 6
  • 2
    @Scuzzy, the question is not clear. Both checked and unckecked checkboxes should be drawn with this code. Try to describe what you want to get in the end. – 7-zete-7 Dec 27 '21 at 20:50
  • @7-zete-7 your solution works in storing the checkboxes. But, I also want to store the unchecked values. So, I can later pull and display them on the update page. – Shun Zu Dec 27 '21 at 20:54
  • @7-zete-7 as, the checkbox values are dynamic. They change based on certain criteria. Therefore, I want to store both. So, the respective values can be easily be retrieved while performing update. Hope that makes sense. fyi: I solved the other issue regarding the update page. It was not updating bcz the id variable was before the allowance variable. rearranging solved the issue. – Shun Zu Dec 27 '21 at 20:57
  • Checkboxes either exist in the submission data because they are checked, or they don't exist in the submission payload because they weren't checked. – mickmackusa Dec 27 '21 at 21:30

1 Answers1

1

In this case, it is necessary to change the structure of the stored data. from an enumeration like "1, 2, 3" it is impossible to understand which value is checked and which is unchecked.

For example use JSON and storing data as like ["1":"1","2":"0","3":"1"]:

$input_item = isset($_POST['item']) ? json_encode($_POST['item']) : '';
$selectedCheckboxes = json_decode($input_item, true);

$idx = 0;
while($row = mysqli_fetch_array($result)) {
  $itemName = $row['item'];
  $isChecked = isset($selectedCheckboxes[$itemName]) && $selectedCheckboxes[$itemName];

  echo "<p>
      <input type='hidden' name='item[{$itemName}]' value='0'>
      <input type='checkbox' name='item[{$itemName}]' value='1' ".($isChecked ? 'checked' : '')."> ".$itemName."</p>
    ";
}
7-zete-7
  • 712
  • 4
  • 12
  • Thank you. This is working to some extent. The only issue I am having is that it is adding check attribute to the uncheck values as well. So, when I pull the values. they all have a tick in the checkbox even the unchecked ones. – Shun Zu Dec 27 '21 at 21:27
  • `isset($selectedCheckboxes[$itemName]) && $selectedCheckboxes[$itemName]` can be simplified to `!empty($selectedCheckboxes[$itemName])` – mickmackusa Dec 27 '21 at 21:33
  • `while($row = mysqli_fetch_array($result)) {` can be functionless: `foreach ($result as $row) {`. – mickmackusa Dec 27 '21 at 21:34
  • Exact duplicate advice from 7 years ago. https://stackoverflow.com/a/25987030/2943403 Please close duplicate questions instead of answering them. – mickmackusa Dec 27 '21 at 21:35
  • 1
    @mickmackusa, please read the question carefully. The questioner has not only a question from the answer you suggested, but also a question about the data storage structure. – 7-zete-7 Dec 28 '21 at 05:47
  • 1
    @mickmackusa, I agree, the code can be simplified or made more beautiful. But will the questioner understand it after such modifications? In the answer, I leave its code, adding the required actions in it. – 7-zete-7 Dec 28 '21 at 05:50