Jump to content

Validating Select boxes


lowe_22

Recommended Posts

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.
Link to comment
Share on other sites

I'm not 100% sure what you are wanting, but this is my best guess:

[code]<?php
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>";
}

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]<?php

echo "<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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.