如何仅在选中相应复选框的文本框中插入值?

时间:2022-11-29 20:51:08

In my program, I have 4 checkboxes that correspond to 4 text fields. The PHP script should insert values into my database only where the checkboxes are checked. Unfortunately, it only works properly if all of the checkboxes are checked. Why is this?

在我的程序中,我有4个复选框,对应4个文本字段。 PHP脚本应仅在选中复选框的位置将值插入到我的数据库中。不幸的是,只有选中了所有复选框,它才能正常工作。为什么是这样?

Here is the testchk.html

这是testchk.html

<html>
<head>
<title>testchk</title>
</head>
<body bgcolor="pink">
<h3> choice item</h3>
<form action="testchk.php" method="post">
    <input type="checkbox" name="chk1[]" value="vegetables">vegetables
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="frozen">frozen products
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="dried">dried goods
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="cooling">
    <input type="text" name="temperature[]"><br />
    <br>
    <input type="submit" name="submit" value="submit">
    </form>
</body>
</html>

and here is the testchk.php

这是testchk.php

include ("db.php");

$checkbox1 = $_POST['chk1'];
$temperature = $_POST['temperature'];
if ($_POST["submit"]=="submit") {
    for ($i=0; $i<sizeof($checkbox1); $i++) {
        $sql="INSERT INTO test (Name, temp) VALUES ('".$checkbox1[$i]."', '".$temperature[$i]."' )";  
        mysql_query($sql) or die(mysql_error());

    }
    echo "insert";
}

1 个解决方案

#1


This is because of the difference between how checkboxes and text inputs are handled. Only the checkboxes that you have checked will be submitted with your form, but all of the text inputs will be submitted whether you have entered anything into them or not. When you name the inputs using [], PHP will generate numeric indices for those arrays. This means that if you do not check all the checkboxes, the index numbers for the checkboxes will not match up with the index numbers for the text inputs. You can fix this by explicitly defining the index numbers in your HTML:

这是因为复选框和文本输入的处理方式不同。只有您选中的复选框才会随表单一起提交,但无论您是否输入了任何内容,都会提交所有文本输入。当您使用[]命名输入时,PHP将为这些数组生成数字索引。这意味着如果不选中所有复选框,则复选框的索引号将与文本输入的索引号不匹配。您可以通过在HTML中显式定义索引号来解决此问题:

<input type="checkbox" name="chk1[0]" value="vegetables">vegetables
<input type="text" name="temperature[0]"><br>
<input type="checkbox" name="chk1[1]" value="frozen">frozen products
<input type="text" name="temperature[1]"><br>
<input type="checkbox" name="chk1[2]" value="dried">dried goods
<input type="text" name="temperature[2]"><br>
<input type="checkbox" name="chk1[3]" value="cooling">
<input type="text" name="temperature[3]"><br>

Then you can use foreach instead to only deal with the ones where the checkbox was checked.

然后你可以使用foreach代替只处理选中复选框的那些。

foreach($checkbox1 as $index => $column_name) {
    // Do your query using $temperature[$index];
}

#1


This is because of the difference between how checkboxes and text inputs are handled. Only the checkboxes that you have checked will be submitted with your form, but all of the text inputs will be submitted whether you have entered anything into them or not. When you name the inputs using [], PHP will generate numeric indices for those arrays. This means that if you do not check all the checkboxes, the index numbers for the checkboxes will not match up with the index numbers for the text inputs. You can fix this by explicitly defining the index numbers in your HTML:

这是因为复选框和文本输入的处理方式不同。只有您选中的复选框才会随表单一起提交,但无论您是否输入了任何内容,都会提交所有文本输入。当您使用[]命名输入时,PHP将为这些数组生成数字索引。这意味着如果不选中所有复选框,则复选框的索引号将与文本输入的索引号不匹配。您可以通过在HTML中显式定义索引号来解决此问题:

<input type="checkbox" name="chk1[0]" value="vegetables">vegetables
<input type="text" name="temperature[0]"><br>
<input type="checkbox" name="chk1[1]" value="frozen">frozen products
<input type="text" name="temperature[1]"><br>
<input type="checkbox" name="chk1[2]" value="dried">dried goods
<input type="text" name="temperature[2]"><br>
<input type="checkbox" name="chk1[3]" value="cooling">
<input type="text" name="temperature[3]"><br>

Then you can use foreach instead to only deal with the ones where the checkbox was checked.

然后你可以使用foreach代替只处理选中复选框的那些。

foreach($checkbox1 as $index => $column_name) {
    // Do your query using $temperature[$index];
}