简体   繁体   English

如何使用php从多个复选框动态获取单个值?

[英]How to get individual values from multiple checkbox dynamically using php?

I am trying to get the individual values from the checkbox , $mail_aspects['active'] and $mail_aspects['aspect_id'] . 我试图从复选框 $mail_aspects['active']$mail_aspects['aspect_id']获取单个值。

But using the below code, I am only able to get the values whose $mail_aspects['active']=1 . 但是使用下面的代码,我只能获得$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 使用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]");
    }   
}

To receive unchecked checkbox values in $_POST you have to add a hidden input field with same name and null value before adding the checkbox. 要在$ _POST中接收未选中的复选框值,必须在添加复选框之前添加具有相同名称和空值的隐藏输入字段。 If a checkbox is unchecked the null value of hidden input will be submitted otherwise the value of the checkbox. 如果未选中该复选框,则将提交隐藏输入的空值,否则将提交该复选框的值。

In your case it should look like: 在您的情况下,它应类似于:

<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="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 is that the names of the hidden and the checkbox input are identically. 重要的是,隐藏和复选框输入的名称是相同的。

Your PHP code should work properly. 您的PHP代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM