-2

I have multiple checkboxes in a foreach loop in my code

<form method='POST' action='save.php'>
<?php foreach($problems as $problem): ?>
<input type='text' name="month[]"/>
<input type="checkbox" name="is_increased[]" value="1" />
<?php endforeach; ?>
<input type='submit' value='Submit'/>
</form>

When I save, I got values of checked checkboxes only. How to get all the checkboxes value, it will be '0' if the checkbox is unchecked. Thank you.

Iftakharul Alam
  • 3,201
  • 4
  • 21
  • 33

2 Answers2

0

Unchecked boxes are not sent in the post. Catch it in your save.php using:

if (isset($_POST['checkbox']))
{ it is checked }
else
{ not checked }
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
0

You have to do something like this:

<form method='POST' action='save.php'>
<?php $i = 0; foreach($problems as $problem): ?>
<input type='text' name=month[<?php echo $i ?>]/>
<input type='hidden' name="is_increased[<?php echo $i ?>]" value="0" />
<input type="checkbox" name="is_increased[<?php echo $i ?>]" value="1" />
<?php ++$i; endforeach; ?>
<input type='submit' value='Submit'/>
</form>

if checkbox is checked, its value is sent to server, but if it is not checked, hidden's value is sent to server