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

落花浮王杯 提交于 2020-01-15 18:51:53

问题


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>

回答1:


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>



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/6752831/how-to-remember-posted-fields-in-forms-for-drop-down-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!