Note that there are some explanatory texts on larger screens.

plurals
  1. PORemembering options in a select box array after submitting through php
    text
    copied!<pre><code>&lt;form method="post"&gt; &lt;select name="box[]"&gt; &lt;option value="1" &lt;?php if ($_POST['box[0]'] == "1") echo "selected=\"selected\";"?&gt;&gt;1&lt;/option&gt; &lt;option value="2" &lt;?php if ($_POST['box[0]'] == "2") echo "selected=\"selected\";"?&gt;&gt;2&lt;/option&gt; &lt;option value="3" &lt;?php if ($_POST['box[0]'] == "3") echo "selected=\"selected\";"?&gt;&gt;3&lt;/option&gt; &lt;/select&gt; &lt;p&gt; &lt;select name="box[]"&gt; &lt;option value="1" &lt;?php if ($_POST['box[1]'] == "1") echo "selected=\"selected\";"?&gt;&gt;1&lt;/option&gt; &lt;option value="2" &lt;?php if ($_POST['box[1]'] == "2") echo "selected=\"selected\";"?&gt;&gt;2&lt;/option&gt; &lt;option value="3" &lt;?php if ($_POST['box[1]'] == "3") echo "selected=\"selected\";"?&gt;&gt;3&lt;/option&gt; &lt;/select&gt; &lt;p&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>When I use box names "box1" and "box2", it works without a problem. What am I doing wrong?</p> <p><em><strong></em>**<em>*</em>**<em>*</em></strong> EDIT <strong><em>*</em>**<em>*</em>****</strong></p> <p>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:</p> <pre><code>&lt;form method="post"&gt; &lt;select name="box[]"&gt; &lt;option value="1" &lt;?php if ($box[0] == "1") echo "selected='selected'";?&gt;&gt;1&lt;/option&gt; &lt;option value="2" &lt;?php if ($box[0] == "2") echo "selected='selected'";?&gt;&gt;2&lt;/option&gt; &lt;option value="3" &lt;?php if ($box[0] == "3") echo "selected='selected'";?&gt;&gt;3&lt;/option&gt; &lt;/select&gt; &lt;p&gt; &lt;select name="box[]"&gt; &lt;option value="1" &lt;?php if ($box[1] == "1") echo "selected='selected'";?&gt;&gt;1&lt;/option&gt; &lt;option value="2" &lt;?php if ($box[1] == "2") echo "selected='selected'";?&gt;&gt;2&lt;/option&gt; &lt;option value="3" &lt;?php if ($box[1] == "3") echo "selected='selected'";?&gt;&gt;3&lt;/option&gt; &lt;/select&gt; &lt;p&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload