如何在PHP中获取选中和未选中复选框数组的所有值并保存到数据库中

人气:1,095 发布:2022-10-16 标签: php html jquery arrays checkbox

问题描述

在获取选中和取消选中的复选框的值时遇到问题。选中复选框时,它将获取该特定复选框的值,而每当取消选中该复选框时,未选中复选框的值将为0。

这是我的html代码:

<table border="0" cellspacing="0" cellpadding="1">
        <tbody>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="5"> Barangay business permit </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="10"> Business plate </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="20"> Sanitary inspection fee </td>
        </tr>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="30"> Environmental clearance </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="40"> Barangay development </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="50"> building permit </td>
        </tr>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="60"> Electrical inspection </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="70"> Mechanical inspection    </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="80"> Plumbing inspection </td>
        </tr>
        </tbody>
</table>
<div class="" style="margin: auto; padding-top:20px;">Total <input type="text" name="total" style="color: #222222;" readonly/>
<input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;">
</form>

PHP代码:

$checkbox=$_POST['checkbox'];

    for($i=0; $i<count($checkbox); $i++)
    {
        $num[$i]=$checkbox[$i];
    }

Java代码(用于在合计字段中检查并生成合计时获取每个复选框的值的函数。)

<script type="text/javascript">
    $(function(){

        var applications = [];
        //bind the change event to the checkboxes
        $('input[name="checkbox[]"]').change(function(){
            var total = 0;
            //get value from each selected ckeck box
            $('input[name="checkbox[]"]:checked').each(function(){
                var tval = $(this).val();
                total += parseFloat(tval);
            });
            //finally display the total with a ₱ sign
            $('input[name="total"]').val("₱ " + total);

            // handle applications
            var application = $.trim($(this)[0].nextSibling.nodeValue); // get the name
            var i = applications.indexOf(application);
            if(i != -1) {
                applications.splice(i, 1); // remove if unchecked
            } else {
                applications.push(application); // push if checked
            }
            var temp = applications.join(', ');
            $('input[name="applications"]').val(temp);

        });

    });
    </script>
在实际测试中,我选中了表[0,1,0,0,1,1,0,0,0]中的一些复选框。注意:1表示勾选,0表示未勾选,以示清楚。

当我单击提交按钮并显示这些值时,我得到了这个

[0]=>10

[1]=>40

[2]=>50

但我想获得这样的值

[0]=>0

[1]=>10

[2]=>0

[3]=>0

[4]=>40

[5]=>50

[6]=>0

[7]=>0

[8]=>0

如何获取这些值?我需要得到未选中的每个值作为0和和;校验值,然后保存每个数组(按顺序)到数据库。我真的需要帮助,我从网上搜索了一下,但我没有找到正确的解决方案。我想我的php代码错了。

数据库名称:应用程序

表:费用

列:ID、Fee1、Fee2、Fee3、Fee4、Fee5、Fee6、Fee7、Fee8、Fee9

推荐答案

从外观上看,您可以在上面使用array_replace

首先通过array_fill()构建默认结构(因为未选中的复选框不会包含在文章中)。然后最后使用array_replace作为默认输入。示例:

Demo

<?php

$default = array_fill(0, 9, 0);
if(isset($_POST['register'])) {
    $input = $_POST['checkbox'];
    $final = array_replace($default, $input);
    echo '<pre>';
    print_r($final);
}

?>
<form method="POST">
    <table border="0" cellspacing="0" cellpadding="1">
            <tbody>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[0]" value="5" class="toggle_checkbox" /> Barangay business permit </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[1]" value="10" class="toggle_checkbox" /> Business plate </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[2]" value="20" class="toggle_checkbox" /> Sanitary inspection fee </td>
            </tr>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[3]" value="30" class="toggle_checkbox" /> Environmental clearance </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[4]" value="40" class="toggle_checkbox" /> Barangay development </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[5]" value="50" class="toggle_checkbox" /> building permit </td>
            </tr>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[6]" value="60" class="toggle_checkbox" /> Electrical inspection </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[7]" value="70" class="toggle_checkbox" /> Mechanical inspection    </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[8]" value="80" class="toggle_checkbox" /> Plumbing inspection </td>
            </tr>
            </tbody>
    <input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;">
    </table>
</form>

Sideote:您需要显式地将索引放在名称属性上,以便在接受替换时它们将对齐。

然后在您的JS中:

由于索引的原因,无法使用名称调用,请将其改为classes

$('.toggle_checkbox').change(function(){
    var total = 0;
    //get value from each selected ckeck box
    $('.toggle_checkbox:checked').each(function(){

899