在表单提交验证后重新填充下拉菜单选择 [英] Refilling drop down menu selection after form submission validation

查看:131
本文介绍了在表单提交验证后重新填充下拉菜单选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在php中注册/注册表单,如果一切都无法正确验证,则重新提交/保留用户输入。我有文本框,密码输入和单选按钮,所有工作,但这些下拉菜单已经更麻烦了。我使用的PHP代码工作在文本框,但不是这些选择/选项,是否有更好的方法来做到这一点?我已经删除了大部分选项,以节省空间,但分别从0-11个月,1-31天和1900-2013年分别。

 < select id =monthname =monthvalue =<?php 
if(isset($ _ POST ['month']))
echo htmlspecialchars($ _ POST ['month'])?>>

< option value =default> Month< / option>
< option value =0> January< / option>
...
< option value =11> December< / option>

< / select>
< select id =formDayname =dayvalue =<?php
if(isset($ _ POST ['day']))
echo htmlspecialchars($ _ POST [ '天'])>?>

< option value =default> Day< / option>
< option value =1> 1< / option>
...
< option value =31> 31< / option>

< / select>
< select id =formYearname =yearvalue =<?php
if(isset($ _ POST ['year']))
echo htmlspecialchars($ _ POST [ '年'])>?>

< option value =default> Year< / option>
< option value =2013​​> 2013< / option>
...
< option value =1900> 1900< / option>

< / select>


解决方案

您可以尝试这个动态生成值>

日:

  echo< select命名= '日' >中; ($ i = 1; $ i $ = 31; $ i ++)

$ b $ selectedDay = isset($ _ POST ['day'])&& $ _POST ['day'] == $ i? 'selected =selected':'';
echo< option $ selectedDay value = $ i> $ i< / option>;
}
echo< / select>;

月份:

  $ months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug' 9月','十月','十一月','十二月'); 
echo< select name ='month'>;
($ i = 0; $ i <= 11; $ i ++)
{
$ m = $ months [$ i];
$ selectedMonth = isset($ _ POST ['month'])&& $ _POST ['month'] == $ i? 'selected =selected':'';
echo< option $ selectedMonth value = $ i> $ m< / option>;
}
echo< / select>;

年份:

  echo< select name ='year'>; 
($ i = 2013; $ i> = 1900; $ i--)
{
$ selectedYear = isset($ _ POST ['year'])& $ _POST ['year'] == $ y? 'selected =selected':'';
echo< option $ selectedYear value = $ i> $ i< / option>;
}
echo< / select>;

演示正常演示选择


I'm working on a sign up/registration form in php that resubmits/retains the users input if everything doesn't validate properly. I've got text box, password input, and radio buttons all working but these drop down menus have been more trouble. The php code I used works for the text boxes but not these select/options, is there a better way to do this? I've cut out the majority of the options just to save space, but each goes from 0-11 months, 1-31 days, and 1900-2013 years respectively.

<select id="month" name="month" value="<?php
    if(isset($_POST['month']))
        echo htmlspecialchars($_POST['month'])?>">

    <option value="default">Month</option>
    <option value="0">January</option>
    ...
    <option value="11">December</option>

</select>
<select id="formDay" name="day" value="<?php
    if(isset($_POST['day']))
        echo htmlspecialchars($_POST['day'])?>">

    <option value="default">Day</option>
    <option value="1">1</option>
    ...
    <option value="31">31</option>

</select>
<select id="formYear" name="year" value="<?php
    if(isset($_POST['year']))
        echo htmlspecialchars($_POST['year'])?>">

    <option value="default">Year</option>
    <option value="2013">2013</option>
    ...
    <option value="1900">1900</option>

 </select>                    

解决方案

You may try this, generate values dynamically

Day:

echo "<select name='day'>";
for( $i = 1; $i <= 31; $i++ )
{
    $selectedDay = isset($_POST['day']) && $_POST['day'] == $i ? 'selected="selected"' : ''; 
    echo "<option $selectedDay value=$i>$i</option>";
}
echo "</select>";

Month:

$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
echo "<select name='month'>";
for( $i = 0; $i <= 11; $i++ )
{
    $m = $months[$i];
    $selectedMonth = isset($_POST['month']) && $_POST['month'] == $i ? 'selected="selected"' : ''; 
    echo "<option $selectedMonth value=$i>$m</option>";
}
echo "</select>";

Year:

echo "<select name='year'>";
for( $i = 2013; $i >= 1900; $i-- )
{
    $selectedYear = isset($_POST['year']) && $_POST['year'] == $y ? 'selected="selected"' : ''; 
    echo "<option $selectedYear value=$i>$i</option>";
}
echo "</select>";

Demo Normal and Demo Selected

这篇关于在表单提交验证后重新填充下拉菜单选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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