lowe_22 Posted November 2, 2006 Share Posted November 2, 2006 Hi,I'm having some trouble with a select box in PHP so that if tere is an error with the selected item, the form keeps the selected item selected to stop the user making the same mistake again.Here is the validation code for the select box:[code]if(!in_array($rank, $highranks) and ($starcolor!="-- Select a Color --")) { $error=true; $err_starcolor="<span class=error>The member is not of sufficient rank for a star color!</span>"; }[/code]Here is the code im using as the select box:[code]<select name="starcolor"> <option value="-- Select a Color --">-- Select a Color --</option> <? foreach($starlist as $val) { echo "<option value=" . if ($err_starcolor){ echo $starcolor } . ">" . $val . "</option>"; } ?> </select> <? echo "<span class=error>".$err_starcolor."</span>"; ?>[/code]I know that won't even work in principle, but I don't know how to do it!Many thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 2, 2006 Share Posted November 2, 2006 I'm not 100% sure what you are wanting, but this is my best guess:[code]<?phpif(!in_array($rank, $highranks) and ($starcolor!="-- Select a Color --")) { $error=true; $err_starcolor="<span class=error>The member is not of sufficient rank for a star color!</span>";}echo "<select name=\"starcolor\">";echo "<option value=\"-- Select a Color --\">-- Select a Color --</option>";foreach($starlist as $val) { echo "<option value=\"" . $val . "\""; echo ($_POST[starcolor]) ? "selected" : ""; echo ">" . $val . "</option>";}echo "</select>";echo "<span class=\"error\">".$err_starcolor."</span>";?>[/code] Quote Link to comment Share on other sites More sharing options...
lowe_22 Posted November 2, 2006 Author Share Posted November 2, 2006 Hmm, It doesn't keep the same value...You know in a standard input field, you can assign the value to keep what the user just entered incorrcetly, eg][code]<input type="text" name="initials" class="text" value="<? if ($initials_err) {echo $initials; } ?>" />[/code]Basically, I need the same thing to happen but for a select box. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 3, 2006 Share Posted November 3, 2006 Sorry, left a bit out. Anyway, this is written with the assumption that "$_POST[starcolor]" was the value that the use submitted. If not, change accordingly.Also, when you set the error text you are placing the text within spans with the 'error' class. then you put that text inside another set of spans with the 'error' class. I changed for simplicity. As well, I changed the 'default' value to have an empty string. Lastly, you need to use '&&' within your if statement - not the word 'and' - I didn't catch that before.[code]<?phpecho "<select name=\"starcolor\">\n";echo "<option value=\"\">-- Select a Color --</option>\n";foreach($starlist as $val) { echo "<option value=\"" . $val . "\""; echo ($_POST[starcolor]==$val) ? "selected" : ""; echo ">" . $val . "</option>\n";}echo "</select>\n";echo "<span class=\"error\">";if(!in_array($rank, $highranks) && $starcolor) { echo "The member is not of sufficient rank for a star color!";}echo "</span>\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.