Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交数组复选框(尽管未选中)_Php_Html - Fatal编程技术网

Php 提交数组复选框(尽管未选中)

Php 提交数组复选框(尽管未选中),php,html,Php,Html,我有一些多值输入,其中有一些复选框。当我提交表单时,如果未选中复选框,它会将其余数据上移,并将输入错误地放在其他行中 例如,如果这两个复选框在中间没有被检查,当我使用Frach循环时,它使用第二行中的第四个危险输入,并且数据在数据库中被错误放置。 这是我的密码: <td> <input type="text" name="description_of_goods_fa[]" class="form-control"> </td>

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

例如,如果这两个复选框在中间没有被检查,当我使用Frach循环时,它使用第二行中的第四个危险输入,并且数据在数据库中被错误放置。 这是我的密码:

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

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

至少有两种解决方案

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>
2除了复选框外,您还可以放置一个名为hazardous的隐藏输入

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

有一种很好的方法可以解决您的任务,还可以提高代码的可读性。只需将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>    

请参阅下面的html代码。对于循环值,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; 
}

这将对你有完全的帮助。

你能发布你的PHP代码吗?请分享完整的代码。Thanks@MichelleCantin我的问题更多的是因为没有提交未选中的复选框,所以它在foreach循环的行中错误放置了输入。@HassanSiddiqui我的问题更多的是因为没有提交未选中的复选框,所以它在foreach循环的行中错误放置了输入。foreach循环在PHP中,对吗?好的,那么请显示PHP1这个页面是完全动态的,它使用jquery创建html行,所以我不能传递索引。在使用jquery创建元素时,是什么阻止您设置name\index?
<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>    
<?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?>">
$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; 
}