html - How to get individual values from multiple checkbox dynamically using php? -


i trying individual values checkbox, $mail_aspects['active'] , $mail_aspects['aspect_id'].

but using below code, able values $mail_aspects['active']=1. need both checked , unchecked values update in db.

can on issue?

<form method='post' style='display: inline-block;'>     <div class="col-md-12">         <?foreach ($customer['mail_aspects'] $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 , name         $stmt=$db->exec("update customer_preferences set active=$exp[0] customer_id=$customerid , aspects_id=$exp[1]");     }    } 

to receive unchecked checkbox values in $_post have add hidden input field same name , null value before adding checkbox. if checkbox unchecked null value of hidden input submitted otherwise value of checkbox.

in case should like:

<form method='post' style='display: inline-block;'> <div class="col-md-12">     <?foreach ($customer['mail_aspects'] $mail_aspects)     { ?>         <table>             <tr>                 <td>                     <label>                          <input type="hidden" name="mail_aspects[<?=$mail_aspects['aspects_id']?>]" value="0" />                         <input type="checkbox" name="mail_aspects[<?=$mail_aspects['aspects_id']?>]" <?=($mail_aspects['active'] == '1') ? 'checked' : ''?>                         value="1">                         <?=$mail_aspects['aspects_name']?>                     </label>                 </td>             </tr>         </table>      <? }?> </div> <button type='submit' name='submit' value='submit'>save aspects</button> </form> 

important names of hidden , checkbox input identically.

your php code should work properly.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -