0

I'm working on a php form with some validation rules.

If some of the fields are left blank that are require the form will report and error message and ask the user to complete the field.

The form currently keeps the values of the $_POST data so that if errors occur the data which is in the field remains.

I am having a problem with two fields(drop down lists) which are populated with data from a database. If I complete these fields but there is a error elsewhere the form displays with the values in the drop down list but when I correct the errors and try submit the form it tells me that the drop down list fields are empty.

Here is the code for them

<! Drop Down Menu to get student names from database !>
<SELECT NAME=studentName >
<OPTION VALUE=0 selected="selected" >
<?php if(isset($_POST['studentName'])) echo $_POST['studentName'];?>
<?php echo $options1?>
</SELECT>  

Any idea's why this is happening?

1
  • What is <! Drop Down Menu to get student names from database !> ...? Commented Sep 2, 2011 at 10:59

2 Answers 2

1

For starters, <option value=0 selected="selected"> means that option 0 will always be selected.

You could do something like the following, excuse me if this snippet has bugs, but it should give you a general idea of how you could achieve what you want to:

<?php
    $myOptions = array(
        '0' => ' --- nothing selected ---',
        '1' => 'Apples',
        '2' => 'Bananas',
        // ...
    );
?>
<select name="studentName">
    <? php
    foreach($myOptions as $key => $value ) {
        $selected = isset($_POST['theOption']) && $_POST['theOption'] == $key ? 'selected="selected"' : "";
        echo '<option value="' . $key . '" '. $selected . '>' . $value . "</option>";
    }
    ?>
</select>

You can cleanup the above a bit using the alternative PHP syntax:

http://php.net/manual/en/control-structures.alternative-syntax.php

This isn't part of your bug, but just a good practice consideration, some of your HTML tags and their attributes are uppercase, and it's common nowadays to use lowercase. It's more standard this way.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the advice. I will try this and see how I get on. PS: Yes I know about about the code looking awful but concentrating on getting it working before I do the house work on it
1

You shouldn't echo $_POST['studentName']; you should check it against the value of each of your options (which should be an array in PHP, which you convert to a list of <option>s in a foreach loop), and set the selected attribute if it matches.

On a sidenote, your HTML is invalid; some attributes are not quoted, and a comment should be in <!-- ... -->, not <! ... !>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.