0

我想创建一个下拉列表,允许用户选择开始和结束“年、月、日”,如下所示。当页面更新时,选项将恢复为“原始”选项。当页面更新时,如何允许这些选定的值保存并保留?

<?php

if (isset($_POST['action']))
{
    $action = $_POST['action'];
    $startyear = $_POST['startyear'];
    $startmonth = $_POST['startmonth'];
    $startday = $_POST['startday'];
    $endyear = $_POST['endyear'];
    $endmonth = $_POST['endmonth'];
    $endday = $_POST['endday'];
}
else
{
    $action = "";
    if (!isset($_POST['startyear']) || !isset($_POST['startmonth']) || !isset($_POST['startday']) 
        || !isset($_POST['endyear']) || !isset($_POST['startmonth']) || !isset($_POST['startday']))
    {
        $startyear = "";
        $startmonth = "";
        $startday = "";
        $endyear = "";
        $endmonth = "";
        $endday = "";
    }
    else
    {
        $startyear = $_POST['startyear'];
        $startmonth = $_POST['startmonth'];
        $startday = $_POST['startday'];
        $endyear = $_POST['endyear'];
        $endmonth = $_POST['endmonth'];
        $endday = $_POST['endday'];
    }

}

if ($action == "update")
{
    echo "<h3>Start: $startyear-$startmonth-$startday | End: $endyear-$endmonth-$endday</h3>";
}

else
{
    if ($startyear == "" || $endyear == "")
    {
    // something belongs here...not sure
    }
    echo "<h3>Start: $startyear-$startmonth-$startday | End: $endyear-$endmonth-$endday</h3>";
?>

开始/结束年、月、日的选择在这里:

<form name='update' action='testing.php' method='POST'>
Start: <select name='startyear'>
    <option value="1">2012</option>
    <option value="2">2011</option>
    <option value="3">2010</option>
    <option value="4">2009</option>
    <option value="5">2008</option>
    <option value="6">2007</option>
    <option value="7">2006</option>
    <option value="8">2005</option>
    <option value="9">2004</option>
    <option value="10">2003</option>
    <option value="11'>2002</option>
    <option value="12">2001</option>
    <option value="13">2000</option>
    <option value="14">1999</option>
    <option value="15">1998</option>
</select>
<select name='startmonth'>
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
</select>
    <select name='startday'>
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
</select>
<br>
End:<select name ='endyear'>
    <option value="1">2012</option>
    <option value="2">2011</option>
    <option value="3">2010</option>
    <option value="4">2009</option>
    <option value="5">2008</option>
    <option value="6">2007</option>
    <option value="7">2006</option>
    <option value="8">2005</option>
    <option value="9">2004</option>
    <option value="10">2003</option>
    <option value="11'>2002</option>
    <option value="12">2001</option>
    <option value="13">2000</option>
    <option value="14">1999</option>
    <option value="15">1998</option>
</select>
<select name='endmonth'>
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
</select>
    <select name='endday'>
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
</select>
<input type='submit' value='Compare'/>
</form>
 <?php
}
?>
4

2 回答 2

1

您需要向生成页面的 HTML 添加更多内容,以便为下拉菜单设置默认选定值。

你需要这样的东西:

<option value="1" <?php if ($_POST["startyear"] = "1") echo "selected"; ?>>2012</option>

您需要为每个选项执行此操作,以查看此选项的值是否与 from 的值相同$_POST;在这种情况下,您可能需要考虑使用for循环来创建 HTML。

于 2012-07-10T17:01:38.960 回答
1

无需让 php 为您呈现如此大的 HTML,这还允许您确定所选属性是否在选项中呈现。

为了保持表单中选择的值,需要将表单发送回与其呈现的相同 php 脚本action='',并且需要对发布的值进行一些逻辑检查以检查该值是否等于自身,请检查下面的表单。

<?php 
//Build an array of values for your forms
$year  = range(1998,2012);
$month = range(01,12);
$day   = range(01,31);

//Handle POST, just an example
if($_SERVER['REQUEST_METHOD']=='POST'){
    foreach($_POST as $key=>$value){
        if(is_numeric($value)){
            $$key = $value;
        }
    }
    /**
     * All POST keys like: startmonth ect will now be php variables like
     * $startmonth
     */

    //Do somthing with $startmonth ect
}

//A more tidy form
?>
<form name='update' action='' method='POST'>
Start: <select name='startyear'>
    <?php foreach(array_reverse($year) as $y):?>
    <option value="<?=$y?>"<?=((isset($startyear) && $startyear == $y)?' selected':null)?>><?=$y?></option>
    <?php endforeach;?>
</select>
<select name='startmonth'>
    <?php foreach($month as $m): $m = str_pad($m, 2, "0", STR_PAD_LEFT);?>
    <option value="<?=$m;?>"<?=((isset($startmonth) && $startmonth == $m)?' selected':null)?>><?=$m;?></option>
    <?php endforeach;?>
</select>
    <select name='startday'>
    <?php foreach($day as $d): $d = str_pad($d, 2, "0", STR_PAD_LEFT);?>
    <option value="<?=$d;?>"<?=((isset($startday) && $startday == $d)?' selected':null)?>><?=$d;?></option>
    <?php endforeach;?>
</select>
<br>
End: <select name='endyear'>
    <?php foreach(array_reverse($year) as $y):?>
    <option value="<?=$y?>"<?=((isset($endyear) && $endyear == $y)?' selected':null)?>><?=$y?></option>
    <?php endforeach;?>
</select>
<select name='endmonth'>
    <?php foreach($month as $m): $m = str_pad($m, 2, "0", STR_PAD_LEFT);?>
    <option value="<?=$m;?>"<?=((isset($endmonth) && $endmonth == $m)?' selected':null)?>><?=$m;?></option>
    <?php endforeach;?>
</select>
    <select name='endday'>
    <?php foreach($day as $d): $d = str_pad($d, 2, "0", STR_PAD_LEFT);?>
    <option value="<?=$d;?>"<?=((isset($endday) && $endday == $d)?' selected':null)?>><?=$d;?></option>
    <?php endforeach;?>
</select>
<input type='submit' value='Compare'/>
</form>
于 2012-07-10T17:05:31.907 回答