【问题标题】:How to get all the values of the checked and unchecked checkbox array and save it to database in PHP如何获取选中和未选中复选框数组的所有值并将其保存到PHP中的数据库
【发布时间】:2014-11-17 03:42:42
【问题描述】:

我无法获取选中和未选中复选框的值。当复选框被选中时,它将获取该特定复选框的值,而当复选框未选中时,未选中复选框的值将为 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、fee1、fee2、fee3、fee4、fee5、fee6、fee7、fee8、fee9

【问题讨论】:

  • 未选中的不张贴

标签: php jquery html arrays checkbox


【解决方案1】:

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

首先通过array_fill() 构建默认结构(因为未选中的复选框不会包含在 POST 中)。然后最后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>

旁注:您需要在 name 属性上显式放置索引,以便在替换发生时它们将对齐。

然后在你的 JS 中:

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

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

【讨论】:

  • 我编辑了我的问题并添加了 javascript 函数。我尝试了您的代码,但是在检查复选框时自动生成总值不起作用,因为我认为将复选框 [] 名称更改为复选框 [带数字] 会影响 javascript 函数。
  • @Ejardy 只需将.change 更改为$('.toggle_checkbox') 而不是指向name=""
  • @Ejardy 我做了修改再试一次
  • 抱歉回复晚了。您的代码有效!但是如何分配变量中的每个数组以将其保存到数据库中?
  • @Ejardy 通常只是循环它,循环下面是插入查询,或者您也可以构建一个查询,使其惰性多行。
【解决方案2】:
<input type='hidden' name='checkbox[0]' value='0'/>
<input type='checkbox' name='checkbox[0]' value='10'/>

因为名字是一样的,如果存在的话php会用checkbox的值覆盖默认值。

【讨论】:

    【解决方案3】:
    $("#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 !!");
    }
    });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多