提交数组复选框,但未选中

时间:2019-09-28 15:00:11

标签: php html

我有一些多值输入,其中有一些复选框。提交表单时,如果未选中复选框,则它将上移其余数据并将输入错放到其他行中。

enter image description here

例如,如果未在中间选中这两个复选框,则在我使用foreach循环时,它将在第二行中使用第四个<td> <input type="text" name="description_of_goods_fa[]" class="form-control"> </td> <td> <input type="text" name="hs_code[]" required title="HS Code needs to be a 8 digit number" class="form-control" pattern="[0-9]{8}"> </td> <td> <input type="checkbox" name="stackable[]" class="form-control" value="1" checked> </td> <td> <input type="checkbox" name="hazardous[]" class="form-control" onchange="check_hazardous(this);" value="1"> </td> <td> <input type="number" name="un[]" min="0" max="9999" step="1" class="form-control" readonly> </td> <td> <input type="number" name="imco_class[]" min="1" max="9.9" step="0.1" class="form-control" readonly> </td> 输入,并且数据被错放到数据库中。

这是我的代码:

$_POST

然后我使用一个foreach循环在php中通过foreach($_POST['hs_code'] as $key => $hs_code) { $description_of_goods_en = $mysqli->real_escape_string($_POST['description_of_goods_en'][$key]); $description_of_goods_fa = $mysqli->real_escape_string($_POST['description_of_goods_fa'][$key]); $hs_code = $mysqli->real_escape_string($_POST['hs_code'][$key]); $hs_code = $mysqli->real_escape_string($_POST['hs_code'][$key]); $stackable = (isset($_POST['stackable']) ? '1' : '0'); $hazardous = (isset($_POST['hazardous'][$key]) ? '1' : '0'); $un = (isset($_POST['un'][$key]) ? $mysqli->real_escape_string($_POST['un'][$key]) : ""); $imco_class = (isset($_POST['imco_class'][$key]) ? $mysqli->real_escape_string($_POST['imco_class'][$key]) : ""); $no_of_unit = $mysqli->real_escape_string($_POST['no_of_unit'][$key]); $unit_price = $mysqli->real_escape_string($_POST['unit_price'][$key]); $total_price = $mysqli->real_escape_string($_POST['total_price'][$key]); $no_of_packages = $mysqli->real_escape_string($_POST['no_of_packages'][$key]); $kind_of_packages = $mysqli->real_escape_string($_POST['kind_of_packages'][$key]); $pkg_length = $mysqli->real_escape_string($_POST['pkg_length'][$key]); $pkg_width = $mysqli->real_escape_string($_POST['pkg_width'][$key]); $pkg_height = $mysqli->real_escape_string($_POST['pkg_height'][$key]); $pkg_volume = $mysqli->real_escape_string($_POST['pkg_volume'][$key]); $total_volume = $mysqli->real_escape_string($_POST['total_volume'][$key]); $pkg_net_weight = $mysqli->real_escape_string($_POST['pkg_net_weight'][$key]); $pkg_gross_weight = $mysqli->real_escape_string($_POST['pkg_gross_weight'][$key]); $total_net_weight = $mysqli->real_escape_string($_POST['total_net_weight'][$key]); $total_gross_weight = $mysqli->real_escape_string($_POST['total_gross_weight'][$key]); $chargeable_weight = (isset($_POST['chargeable_weight'][$key]) ? $mysqli->real_escape_string($_POST['chargeable_weight'][$key]) : ""); if($hs_code != '' || $no_of_packages != '' || $description_of_goods_en != '' || $volume != '') { $sql = " INSERT INTO `inquery_cargo` SET `id_inquery`='$id_inquiry', `description_of_goods_en`='$description_of_goods_en', `description_of_goods_fa`='$description_of_goods_fa', `hs_code`='$hs_code', `stackable`='$stackable', `hazardous`='$hazardous', `un`='$un', `imco_class`='$imco_class', `no_of_unit`='$no_of_unit', `unit_price`='$unit_price', `total_price` = '$total_price', `no_of_packages`='$no_of_packages', `kind_of_packages`='$kind_of_packages', `pkg_length`='$pkg_length', `pkg_width`='$pkg_width', `pkg_height`='$pkg_height', `pkg_volume`='$pkg_volume', `total_volume`='$total_volume', `pkg_net_weight`='$pkg_net_weight', `pkg_gross_weight`='$pkg_gross_weight', `total_net_weight`='$total_net_weight', `total_gross_weight`='$total_gross_weight', `chargeable_weight`='$chargeable_weight' "; $res = $mysqli->query($sql); if(!$res) { $text = "An error occurred during the process."; $error = true; } } }

|__django-app
    |__tests
        |-- __init__.py
        |-- test_models.py   
        |-- test_views.py   

如何提交未选中的复选框?

3 个答案:

答案 0 :(得分:2)

至少有两种解决方案

1)您应该传递行索引以形成每一行的字段名称。例如

<tr>
...
   <input type="text" name="description_of_goods_fa[1]"
       class="form-control">
...
</tr>

<tr>
...
   <input type="text" name="description_of_goods_fa[2]"
       class="form-control">
...
</tr>

因此,在PHP代码中,您可以在循环中检查索引是否为

$hazardous = isset($_POST['hazardous'][$index]) ? $_POST['hazardous'][$index] : null;

2)您可以在复选框旁边放置一个隐藏的输入,其中包含危险的名称

<input type="checkbox" class="form-control hazardous-checkbox"
       onclick="changeHazardousInput(this)" value="1">
<input type="hidden" name="hazardous[]"
       onchange="check_hazardous(this);">

因此,您只需要编写一个“ changeHazardousInput”函数即可更改同级隐藏输入的值

changeHazardousInput函数的示例:

function changeHazardousInput(checkbox) {
    checkbox.nextElementSibling.value = checkbox.checked ? checkbox.value : '';
}

答案 1 :(得分:1)

有一个很好的方法来解决您的任务并提高代码的可读性。只需将HTML结构更改为将两个复选框都置于同一行号下即可。您仍然不会收到未选中的复选框,但是由于数据被分组在行号下,因此不再是问题。

<tr>
    <td>
        <input type="text" name="line[1][description_of_goods_fa]" class="form-control">
    </td>
    <td>
        <input type="text" name="line[1][hs_code]">
    </td>
    <td>
        <input type="checkbox" name="line[1][stackable]" value="1" checked>
    </td>
    <td>
        <input type="checkbox" name="line[1][hazardous]" onchange="check_hazardous(this);" value="1">
    </td>
</tr>    
<tr>
    <td>
        <input type="text" name="line[2][description_of_goods_fa]" class="form-control">
    </td>
    <td>
        <input type="text" name="line[2][hs_code]">
    </td>
    <td>
        <input type="checkbox" name="line[2][stackable]" value="1" checked>
    </td>
    <td>
        <input type="checkbox" name="line[2][hazardous]" onchange="check_hazardous(this);" value="1">
    </td>
</tr>    

答案 2 :(得分:1)

请参见以下html代码。 for的i的循环值将是动态的或您定义的

<?php for($i=0;$i<5;$i++):?>
            <tr>

                <td>
                    <input type="checkbox" name="stackable[]" class="form-control"
                    value="1" checked>
                </td>
                <td>
                    <input type="checkbox" name="hazardous[]" class="form-control"
                    value="1">
                </td>

            </tr>
        <?php endfor;?>
            <input type="hidden" name="row" value="<?php echo $i?>">

并查看下面用于提交数据的php文件的代码

$row=$_POST['row'];
$stack=$_POST["stackable"];
$hazar=$_POST["hazardous"];
for($i=0;$i<$row;$i++){
    $stackable=(isset($stack[$i]))?1:0;
    $hazardous=(isset($hazar[$i]))?1:0; 
}

它会为您全力帮助。

相关问题