通过php提交后记住选择框数组中的选项 [英] Remembering options in a select box array after submitting through php
问题描述
<form method="post">
<select name="box[]">
<option value="1" <?php if ($_POST['box[0]'] == "1") echo "selected=\"selected\";"?>>1</option>
<option value="2" <?php if ($_POST['box[0]'] == "2") echo "selected=\"selected\";"?>>2</option>
<option value="3" <?php if ($_POST['box[0]'] == "3") echo "selected=\"selected\";"?>>3</option>
</select>
<p>
<select name="box[]">
<option value="1" <?php if ($_POST['box[1]'] == "1") echo "selected=\"selected\";"?>>1</option>
<option value="2" <?php if ($_POST['box[1]'] == "2") echo "selected=\"selected\";"?>>2</option>
<option value="3" <?php if ($_POST['box[1]'] == "3") echo "selected=\"selected\";"?>>3</option>
</select>
<p>
<input type="submit" value="Submit">
</form>
当我使用框名称"box1"和"box2"时,它可以正常工作.我在做什么错了?
When I use box names "box1" and "box2", it works without a problem. What am I doing wrong?
** * ** * 编辑 * * * * ****
****** EDIT ********
非常感谢您的评论,但实际上我自己找到了解决方案,即使这没有多大意义.我没有在if语句中使用$ _POST ['box'] [0]和[1],而是使用$ box [0]和[1].即使已发布,显然php仍将其视为普通数组,而不是某种$ _POST-array!工作代码:
Thanks a lot for your comments, but I actually found the solution myself, even if it doesn't make much sense. Instead of using $_POST['box'][0] and [1] at the if statement, I simply used $box[0] and [1]. Even though it's posted, apparently php sees it as a normal array, and not as some kind of $_POST-array! Working code:
<form method="post">
<select name="box[]">
<option value="1" <?php if ($box[0] == "1") echo "selected='selected'";?>>1</option>
<option value="2" <?php if ($box[0] == "2") echo "selected='selected'";?>>2</option>
<option value="3" <?php if ($box[0] == "3") echo "selected='selected'";?>>3</option>
</select>
<p>
<select name="box[]">
<option value="1" <?php if ($box[1] == "1") echo "selected='selected'";?>>1</option>
<option value="2" <?php if ($box[1] == "2") echo "selected='selected'";?>>2</option>
<option value="3" <?php if ($box[1] == "3") echo "selected='selected'";?>>3</option>
</select>
<p>
<input type="submit" value="Submit">
</form>
推荐答案
非常感谢您的评论,但实际上我自己找到了解决方案,即使这没有多大意义.我没有在if语句中使用$ _POST ['box'] [0]和[1],而是使用$ box [0]和[1].即使已发布,显然php仍将其视为普通数组,而不是某种$ _POST-array!工作代码:
Thanks a lot for your comments, but I actually found the solution myself, even if it doesn't make much sense. Instead of using $_POST['box'][0] and [1] at the if statement, I simply used $box[0] and [1]. Even though it's posted, apparently php sees it as a normal array, and not as some kind of $_POST-array! Working code:
<form method="post">
<select name="box[]">
<option value="1" <?php if ($box[0] == "1") echo "selected='selected'";?>>1</option>
<option value="2" <?php if ($box[0] == "2") echo "selected='selected'";?>>2</option>
<option value="3" <?php if ($box[0] == "3") echo "selected='selected'";?>>3</option>
</select>
<p>
<select name="box[]">
<option value="1" <?php if ($box[1] == "1") echo "selected='selected'";?>>1</option>
<option value="2" <?php if ($box[1] == "2") echo "selected='selected'";?>>2</option>
<option value="3" <?php if ($box[1] == "3") echo "selected='selected'";?>>3</option>
</select>
<p>
<input type="submit" value="Submit">
</form>
这篇关于通过php提交后记住选择框数组中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!