Set control array

I am trying to check the checkbox list until the message is valid, and no errors. Below is the code I'm using. I would be grateful for any help.

     <?php foreach ($drinks_checkbox as $option => $options){  ?>

     <input type='checkbox' id='drinks[]' name='drinks[]' value='<?php echo $option;?>'   <?php if(!empty($_POST['drinks'])){if($_POST['drinks']==$option){ echo "checked='checked'" ; }}?> /><?php echo $options;?><br />
    <?php } ?>

I can successfully display checked checkboxes with implode, but I need help for the above.

+1
source share
1 answer

$ _ POST ['drinks'] is an array. In addition, the id value should not be "drinks []"

Try something like this:

$drinksIndex = 0;
$drinksPost = $_POST['drinks'];
foreach ($drinks_checkbox as $option => $options){  ?>

  <input type='checkbox' id='drinks<?php echo $drinksIndex; ?>' name='drinks[<?php echo $drinksIndex; ?>]' value='<?php echo $option;?>'<?php
  if( !empty($drinksPost[$drinksIndex]) ) echo " checked='checked'";
  $drinksIndex++;
?> /><?php
echo $options;?><br />
<?php
} ?>

Part changed! empty ($ drinksPost [$ drinksIndex]) and changed to associative.

If this does not work, you can enable $ drinks_checkbox

0
source

Source: https://habr.com/ru/post/1626641/


All Articles