0
votes

I need help with form validation. I can get it to work with text boxes but I am having trouble validating drop down lists.

I have included a part of my php code plus the code that is imbedded in the HTML

I am having trouble displaying the required text next to the drop down list. Also how would I display the user selected option if the user forgets to enter an input elsewhere on the form so it doesn't reset the dropdown field and show what the user had already had selected.

<?php     
if(empty($_POST['title']))
            {
                $errors['title1'] = " Required";
            }
    <p>

?>
        <label for="title" class="label"><font color="#040404">Title:</font></label>
                        <select name="title">
                            <option value="" selected="selected">Select title</option>
                            <option value ="Mr">Mr</option>
                            <option value ="Mrs">Mrs</option>
                            <option value="Ms">Ms</option>
                            <option value="Miss">Miss</option>
                            <option value="Dr">Dr</option>
                            </select>                   

    </p>
1
If you're using PHP for Form Validation, this is somewhat more difficult. If you use (say) jQuery for form validation, if something required hasn't been filled in, then the form doesn't get sent so it should not reset the dropdown. - Adam T
Of course you "should" use validation on both the front end "and" the Server Side as well. In terms of avoiding form reset, keep the browser from submitting if a field has not been populated/selected. - Adam T
I have to do this for my senior project and its due tomorrow and I am stuck on getting the dropdown menu to work like the text box do. - Shiv Patel
If you must rely on PHP, then you can use $_SESSION and store a value. When the user submits the form, then update the value of the particular session variable. Then when the page loads, you can use PHP to check if the session variable has been set, and (if so) use that value as the selected option of the dropdown. - Adam T

1 Answers

0
votes

Here is another approach, which assumes that your page loads in, and if the "title" select either has a selection or not. Again, it's just a suggestion to try out.

<select name="title">
<?php
    $titles = array ('Mr', 'Mrs', 'Ms', 'Miss', 'Dr');
    if (isset($_POST['title'])) {
        echo '<option value="">Select title</option>';
        $use_title = $_POST['title'];
    } else {
        // Selection not made, let "Select title" be selected.
        echo '<option value="" selected="selected">Select title</option>';
        $use_title = "";
    }

    //When selected before, it will append the selected attribute.
    foreach ($titles as $ti) {
        $selected_attr = ($ti == $use_title ? ' selected="selected"' : '');
        echo sprintf(
            '<option value="%s"%s>%s</option>'
            $ti,
            $selected_attr
            $ti,
        );
    }
?>
</select>