首页 > 解决方案 > 如何在php中显示多个下拉框

问题描述

我正在创建一个应用程序,要求用户选择三个下拉框,在用户单击提交按钮后,它将显示指定的日期。在我的代码中,我必须在每个下拉框之后放置提交按钮,否则代码不会显示三个下拉列表之一,而是将数字放在常规文本中。我该如何解决?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="cov.php" method="POST">
    <?php
        
    ?>
    <?php
        if($_SERVER["REQUEST_METHOD"]=="GET"){
            echo"<h2>Enter birthdate</h2>";
            echo'<label for="year">Year:</label><select name="year" id="year"><option selected="selected"></option>';
            foreach (range(1921,2021,1) as $Years){  
                echo "<option value='".strtolower($Years)."'>$Years</option>";
            }
            echo'<p><input type="submit" value="Submit"></p>';
            echo'<label for="month">Month:</label><select name="month" id="month"><option selected="selected"></option>';
            foreach (range(1,12,1) as $Month) {  
                echo "<option value='".strtolower($Month)."'>$Month</option>";
            }
            echo'<p><input type="submit" value="Submit"></p>';
            echo'<label for="Day">Day:</label><select name="Day" id="Day"><option selected="selected"></option>';
            foreach (range(1,31,1) as $Day) {  
                echo "<option value='".strtolower($Day)."'>$Day</option>";
            }
            echo'<p><input type="submit" value="Submit"></p>';
        }
        else{
            $Year = (int)$_REQUEST["year"];
            $currentMonth = (int)$_REQUEST["month"];
            $currentDay = (int)$_REQUEST["Day"];

            $Month = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June',
            7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');

            function getSuffix($number) {
                $number = abs($number) % 100;
                $lastChar = substr($number, -1, 1);
                switch ($lastChar) {
                    case '1' : return ($number == '11') ? 'th' : 'st';
                    case '2' : return ($number == '12') ? 'th' : 'nd';
                    case '3' : return ($number == '13') ? 'th' : 'rd'; 
                }
                return 'th';  
            }
            if(empty($Year) AND empty($currentMonth) AND empty($currentDay)){
                echo "You forgot Year<br>";
                echo "You forgot Month<br>";
                echo "You forgot Day<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($Year) AND empty($currentMonth)){
                echo "You forgot Year<br>";
                echo "You forgot Month<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($currentMonth)AND empty($currentDay)){
                echo "You forgot Month<br>";
                echo "You forgot Day<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($Year) AND empty($currentDay)){
                echo "You forgot Year<br>";
                echo "You forgot Day<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($Year)){
                echo "You forgot Year<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($currentMonth)){
                echo "You forgot Month<br>";
                echo "Please go back and fill out";
            }
            elseif(empty($currentDay)){
                echo "You forgot Day<br>";
                echo "Please go back and fill out";
            }
            elseif($Year > 1921 AND $Year < 2021){
                if($currentMonth >= 1 AND $currentMonth <= 12){
                    if ($currentDay >=1 and $currentDay <= 31 and $currentMonth == 1 or $currentMonth == 3 or $currentMonth == 5 or $currentMonth == 7 or $currentMonth == 8 or $currentMonth == 10 or $currentMonth == 12){
                    }
                    elseif ($currentDay >=1 and $currentDay <= 28 and $currentMonth == 2){
                    }
                    elseif ($currentDay >=1 and $currentDay <= 30 and $currentMonth == 4 or $currentMonth == 6 or $currentMonth == 9 or $currentMonth == 11){
                    }
                }
            echo "The birthdate is $Year $Month[$currentMonth] $currentDay" . getSuffix($currentDay);
            }
            else{
                echo "Invalid Input. Please go back and fix it";
            }
        }
    ?>
    </form>
</body>
</html>

标签: phphtml

解决方案


推荐阅读