不要从php中动态创建的复选框中获取正确的值

时间:2022-10-07 17:53:01

I have this dynamically created checkboxes, which I would like to get some data out off, so I can put into my DB. I have just discovered that, that the checkboxex seems not to Work, as I catches the wrong checkbox or something.

我有这个动态创建的复选框,我希望得到一些数据,所以我可以放入我的数据库。我刚刚发现,checkboxex似乎没有工作,因为我抓错了复选框或其他东西。

The checkboxes looks like this:

复选框如下所示:

<td align="center">
<input name="e10[]" type="checkbox" <?php echo $checked = $foo['e10'] == '1' ? "checked=\"checked\"" : "";?> onclick="document.getElementsByName('e11[]')[<?php echo $tæller-1;?>].value = this.checked ? '<?php echo date('d-m-Y');?>' : '';" value="0" />
<input type="text" class="listform" style="width:60px;" name="e11[]" value="<?php echo sqltoalmdatetom($foo['e11']); ?>">

And on my submit page, I have tried this:

在我的提交页面上,我试过这个:

echo "0=>".$_POST['e10'][0]."|";
echo "1=>".$_POST['e10'][1]."|";
echo "2=>".$_POST['e10'][2]."|";

The problem is, that when I only have checked e10[1], then it will show up as it was e10[0]. If I check them both I'll get the right result. What can I do to get the right value, when the first element isn't checked?

问题是,当我只检查e10 [1]时,它会显示为e10 [0]。如果我检查它们,我会得到正确的结果。如果未检查第一个元素,我该怎么做才能获得正确的值?

--------- update --------- So now I can get the value, but how do I use it in my mysql update statement?

---------更新---------所以现在我可以获取值,但是如何在我的mysql更新语句中使用它?

I'm running through my lines ($etape), as each checkbox is on it's one line. When I need the fields for the update, I was hoping I could use $_POST[e10], but I can't do that now can I?. Each line has a dynamic number, in this case starting at 0, so the checkboxes would be named $_POST[e10[i*line number*]]

我正在浏览我的行($ etape),因为每个复选框都在它的一行上。当我需要更新的字段时,我希望我可以使用$ _POST [e10],但我现在不能这样做了吗?每行都有一个动态数字,在这种情况下从0开始,因此复选框将命名为$ _POST [e10 [i *行号*]]

        for ($j=0; $j<=count($etape); $j++) {
        //Vi går kun videre hvis etapenavnet er udfyldt. 
        //Sikre at der ikke oprettes en tom etape, og at man ikke kommer til at slette etapenavnet
        if ($_POST['e1'][$j] != "") {//e1 == etapebeskrivelsesfeltet
            //Så længe vi ikke er kommet til den nye etape/række, så opdatere vi bare de gamle
            if ($j < count($etape)) {
                foreach ($keys as $feltnavn) {
                        //echo $feltnavn;
                        $værdi = $_POST[$feltnavn][$j];
                        $querys[] = "`".$feltnavn."` = \"".addslashes($værdi)."\"";
                        if ($feltnavn == "e25") {
                            $test_e25 = $værdi;
                        }
                        if ($feltnavn == "e3") {
                            $test_e3 = $værdi;
                        }

                }
                if ($test_e3 && $test_e25) {
                    echo $tmp = 'UPDATE `etape` SET '.implode(', ', $querys).' WHERE id = '.$etape[$j].'  AND ordre_id = '.$tilbudsnummer;

                    echo "<br><br>";
                    unset($querys);
                    //mysql_query($tmp) or die(mysql_error());
                }
            }
        }
    }

1 个解决方案

#1


0  

When submitting a form, only checked checkbox values will be submitted along. That means you can't use numeric indexes with checkbox arrays to determine which checkbox has been checked. Use an array with string key values instead. Example

提交表单时,只会提交选中的复选框值。这意味着您不能将数字索引与复选框数组一起使用来确定已选中哪个复选框。请使用带字符串键值的数组。例

HTML:

<input name="e10[i0]" type="checkbox" value="0" />
<input name="e10[i1]" type="checkbox" value="1" />
<input name="e10[i2]" type="checkbox" value="2" />

PHP:

if (is_array($_POST['e10']) {
    foreach($_POST['e10'] as $key => $value) {
        $numeric_key = intval(ltrim($key, 'i'));
        echo "$numeric_key => $value |";
    }
}

#1


0  

When submitting a form, only checked checkbox values will be submitted along. That means you can't use numeric indexes with checkbox arrays to determine which checkbox has been checked. Use an array with string key values instead. Example

提交表单时,只会提交选中的复选框值。这意味着您不能将数字索引与复选框数组一起使用来确定已选中哪个复选框。请使用带字符串键值的数组。例

HTML:

<input name="e10[i0]" type="checkbox" value="0" />
<input name="e10[i1]" type="checkbox" value="1" />
<input name="e10[i2]" type="checkbox" value="2" />

PHP:

if (is_array($_POST['e10']) {
    foreach($_POST['e10'] as $key => $value) {
        $numeric_key = intval(ltrim($key, 'i'));
        echo "$numeric_key => $value |";
    }
}