繁体   English   中英

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

[英]How to get all the values of the checked and unchecked checkbox array and save it to database in PHP

我在获取选中和未选中复选框的值时遇到麻烦。 选中该复选框时,它将获得该特定复选框的值,并且每当取消选中该复选框时,未选中的复选框的值将为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];
    }

Javascript代码(用于在检查和生成总计字段时获取每个复选框的值的功能。)

<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,费用1,费用2,费用3,费用4,费用5,费用6,费用7,费用8,费用9

从外观array_replace ,您可以在此使用array_replace

首先通过array_fill()构建默认结构(因为POST不会包含未选中的复选框)。 然后,最后用输入将array_replace为默认值。 例:

演示

<?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>

旁注:您需要明确地将索引放在name属性上,以便在替换被使用时将它们对齐。

然后在您的JS中:

由于由于索引而无法使用名称调用,因此请将其转换为classes

$('.toggle_checkbox').change(function(){
    var total = 0;
    //get value from each selected ckeck box
    $('.toggle_checkbox:checked').each(function(){
<input type='hidden' name='checkbox[0]' value='0'/>
<input type='checkbox' name='checkbox[0]' value='10'/>

由于名称相同,因此php将使用复选框值覆盖默认值(如果存在)。

$("#save").click(function(){
var check = [];
$('.checkBox:checked').each(function() 
{check.push($(this).val());});
var count=check.length;
var data={"Header":"some data","details":[]};
var detail={"values":""};
for(i=0;i<=count-1;i++){
var val=check[i];
detail.values=val;
data.details.push(detail);
detail={"values":""};
}
$.ajax({
url:"data/save",
data:JSON.stringify(data),
success:function(){
alert("sukses !!");
}
});

});

暂无
暂无

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

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