user784637
user784637

Reputation: 16092

How to remember posted fields in forms for drop down items?

I'm creating a simple registration page:

<?php
    $firstName = $_POST['firstName'];
?>

<form action="registration.php" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>
                <b>First name:</b>
            </td>
            <td>
                <input type="text" name="firstName" size="30" maxlength="400" value="<?php echo $firstName; ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>

    </table>
</form>

If the user enters an invalid first name (ex. too short, or weird symbols), the page reloads with an error message shows up to tell them that they have to re-enter their name HOWEVER, the value of the text field is what they most recently entered. This way whenever a user doesn't pass validation, they don't have to re-enter all the fields in the form.

What's a good way to remember what the user entered for a drop down menu? The problem for me is that the option value is different than the text inside the option tag. So when I use the above approach, if I select "Mar" if in invalid submission occurs, '03' appears in the dropdown menu.

    <select name="birthdayMonth">
        <option value="-1">Month:</option>
        <option value="01">Jan</option>
        <option value="02">Feb</option>
        <option value="03">Mar</option>
        <option value="04">Apr</option>
        <option value="05">May</option>
        <option value="06">Jun</option>
        <option value="07">Jul</option>
        <option value="08">Aug</option>
        <option value="09">Sep</option>
        <option value="10">Oct</option>
        <option value="11">Nov</option>
        <option value="12">Dec</option>
    </select>

Upvotes: 1

Views: 3162

Answers (3)

Paul
Paul

Reputation: 141839

At the top:

$birthdayMonth = $_POST['birthdayMonth']

In the select:

<select name="birthdayMonth">
    <option value="-1">Month:</option>
    <option value="01"<?php echo $birthdayMonth == '01' ? 'selected="selected"' : ''; ?>>Jan</option>
    <option value="02"<?php echo $birthdayMonth == '02' ? 'selected="selected"' : ''; ?>>Feb</option>
    <option value="03"<?php echo $birthdayMonth == '03' ? 'selected="selected"' : ''; ?>>Mar</option>
    <option value="04"<?php echo $birthdayMonth == '04' ? 'selected="selected"' : ''; ?>>Apr</option>
    <option value="05"<?php echo $birthdayMonth == '05' ? 'selected="selected"' : ''; ?>>May</option>
    <option value="06"<?php echo $birthdayMonth == '06' ? 'selected="selected"' : ''; ?>>Jun</option>
    <option value="07"<?php echo $birthdayMonth == '07' ? 'selected="selected"' : ''; ?>>Jul</option>
    <option value="08"<?php echo $birthdayMonth == '08' ? 'selected="selected"' : ''; ?>>Aug</option>
    <option value="09"<?php echo $birthdayMonth == '09' ? 'selected="selected"' : ''; ?>>Sep</option>
    <option value="10"<?php echo $birthdayMonth == '10' ? 'selected="selected"' : ''; ?>>Oct</option>
    <option value="11"<?php echo $birthdayMonth == '11' ? 'selected="selected"' : ''; ?>>Nov</option>
    <option value="12"<?php echo $birthdayMonth == '12' ? 'selected="selected"' : ''; ?>>Dec</option>
</select>

Upvotes: 3

Dmytro Zavalkin
Dmytro Zavalkin

Reputation: 5277

<option value="03" <?php echo $_POST['birthdayMonth'] == '03' ?
'selected="selected"' : '' ?>>Mar</option>

Upvotes: 0

ThatGuyInIT
ThatGuyInIT

Reputation: 2239

This is a really dirty of what doing what you want, but it will work:

http://www.plus2net.com/php_tutorial/pb-drop.php

It would be better to use loop to build the dropdown and then add the selected="selected" value to the correct option.

Upvotes: 0

Related Questions