I am trying to get the individual values from the checkbox,
$mail_aspects['active']
and $mail_aspects['aspect_id']
.
But using the below code, I am only able to get the values whose $mail_aspects['active']=1
. But I need to get both checked and unchecked values to update in DB.
Can anyone help on this issue?
<form method='post' style='display: inline-block;'>
<div class="col-md-12">
<?foreach ($customer['mail_aspects'] as $mail_aspects)
{ ?>
<table>
<tr>
<td>
<label>
<input type="checkbox" name="mail_aspects[]" <?=($mail_aspects['active'] == '1') ? 'checked' : ''?>
value="<?=$mail_aspects['active']?>,<?=$mail_aspects['aspects_id']?>">
<?=$mail_aspects['aspects_name']?>
</label>
</td>
</tr>
</table>
<? }?>
</div>
<button type='submit' name='submit' value='Submit'>Save Aspects</button>
</form>
Using PHP
if(isset($_POST['submit'])){
$mail_aspectsdet=$_POST['mail_aspects'];
for($i=0;$i<count($mail_aspectsdet);$i++)
{
$exp=explode(',',$mail_aspectsdet[$i]);//Explode id and name
$stmt=$db->exec("UPDATE customer_preferences set active=$exp[0] where customer_id=$customerID and aspects_id=$exp[1]");
}
}