复选框"检查" - 值跳起来前面的数组值,当数组为空 [英] Checkbox "checked"-value jumps up to earlier array values, when array is empty
问题描述
我的复选框名称=框[]数组。通过PHP我确保他们检查后,他们提交的呼应检查='检查'如果他们在提交事件检查。
I have an array of checkboxes name="box[]". Through PHP I make sure that they're checked after they're submitted by echoing "checked='checked'" if they were checked at submit event.
现在,如果我检查了第三个框,值跳下来的第一个复选框后提交,因为数组是空的,直到第三个复选框。同样的,如果我检查了第二和第三的复选框,就跳了下去,以第一和第二提交之后。这是code我使用的是:
Now, if I check the third box, the value jumps down to the first checkbox after submit, since the array was empty up until the third checkbox. Same, if I check the 2nd and 3rd checkbox, they jump down to 1st and 2nd after submit. This is the code I'm using:
<form method="post">
<input type="checkbox" name="box[]" value="true" <?php if ($box[0] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[1] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[2] == true) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
试用于:
http://experiencerapanui.com/selecttest.php
我可以做的复选框填补了一个值假或任何数组,如果没有选中该复选框?我应该走哪条路?
Can I make the checkboxes fill up the array with a value "false" or whatever, if the box is unchecked? Which way should I go?
的**的 * 的***的 修改 的**的 * **的 * 的
****** EDIT ******
由于phant0m,我设法想出了一个解决方案:
Thanks to phant0m, I managed to come up with a solution:
<form method="post">
<input type="checkbox" name="box[]" value="1" <?php if (in_array("1", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="2" <?php if (in_array("2", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="3" <?php if (in_array("3", $box)) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
把唯一值的复选框,然后如果我发现在数组$箱[]的值,如检查框标记。
Putting unique values for the checkboxes, then if I find the value in the array $box[], the box is marked as checked.
推荐答案
这是不行的,因为只有那些复选框被选中,都被放进 $箱
阵列。
This does not work, because only those checkboxes, that are checked, are being put into the $box
array.
既可以使用不同的名称,或不同的值来区分它们。
Either use different names, or different values to distinguish between them.
考虑一下:你检查的第二个和第三个复选框。在PHP中,您将获得:
Consider this: You check the second and the third checkbox. In PHP, you will receive:
$_POST['box'] = array(0 => "true", 1 => "true");
您可以不知道,这复选框被选中,除非所有的人都。
You cannot know, which checkboxes have been checked, unless all of them are.
这篇关于复选框&QUOT;检查&QUOT; - 值跳起来前面的数组值,当数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!