如何记住下拉项目的表单中的发布字段? [英] How to remember posted fields in forms for drop down items?

查看:136
本文介绍了如何记住下拉项目的表单中的发布字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的注册页面:

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

< form action =registration.phpmethod =postenctype =multipart / form-data>
< table>
< tr>
< td>
< b>名字:< / b>
< / td>
< td>
< input type =textname =firstNamesize =30maxlength =400value =<?php echo $ firstName;?> />
< / td>
< / tr>
< tr>
< td>
< input type =submitvalue =Submit/>
< / td>
< / tr>

< / table>
< / form>

如果用户输入无效的名字(例如太短或奇怪的符号)重新加载错误消息,告诉他们必须重新输入他们的姓名,但是文本字段的值是他们最近输入的。这种方式每当用户没有通过验证,他们不必重新输入表单中的所有字段。



什么是记住用户输入的下拉菜单?对我来说问题是选项值与选项标签内的文本不同。因此,当我使用上述方法时,如果在出现无效提交时选择Mar,则在下拉菜单中会出现03。

 < select name =birthdayMonth> 
< option value = - 1>月份:< / 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> 8月< / option>
< option value =09> Sep< / option>
< option value =10> Oct< / option>
< option value =11> Nov< / option>
< option value =12> Dec< / option>
< / select>


解决方案

b
$ birthdayMonth = $ _POST ['birthdayMonth']



select

 < select name =birthdayMonth> 
< option value = - 1>月份:< / option>
< option value =01<?php echo $ birthdayMonth =='01'? 'selected =selected':''; ?>>扬< /选项>
< option value =02<?php echo $ birthdayMonth =='02'? 'selected =selected':''; ?>>二月< /选项>
< option value =03<?php echo $ birthdayMonth =='03'? 'selected =selected':''; ?>> MAR< /选项>
< option value =04<?php echo $ birthdayMonth =='04'? 'selected =selected':''; ?>>四月< /选项>
< option value =05<?php echo $ birthdayMonth =='05'? 'selected =selected':''; ?>>五月< /选项>
< option value =06<?php echo $ birthdayMonth =='06'? 'selected =selected':''; ?>>君< /选项>
< option value =07<?php echo $ birthdayMonth =='07'? 'selected =selected':''; ?>>七月及LT; /选项>
< option value =08<?php echo $ birthdayMonth =='08'? 'selected =selected':''; ?>>日及LT; /选项>
< option value =09<?php echo $ birthdayMonth =='09'? 'selected =selected':''; ?>>九月< /选项>
< option value =10<?php echo $ birthdayMonth =='10'? 'selected =selected':''; ?>> 10月< /选项>
< option value =11<?php echo $ birthdayMonth =='11'? 'selected =selected':''; ?>>十一月< /选项>
< option value =12<?php echo $ birthdayMonth =='12'? 'selected =selected':''; ?>> DEC或LT; /选项>
< / select>


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>

解决方案

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>

这篇关于如何记住下拉项目的表单中的发布字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆