如何制作动态的月,日,年错误消息

时间:2019-02-18 16:51:57

标签: php html css dynamic error-handling

在某种程度上,我想使表单的月,日,年错误文本动态化,如果用户未选择月和日,它将显示“请选择年份”。如果用户未选择全部三个,则会显示“请选择月,日和年”。如果用户未选择年份,则会显示“请选择年份”,以此类推...当前,我的代码仅显示“请选择月份”,“请选择日期”,“请选择年份”顺便说一句,我认为我应该修复此问题,以便用户更容易阅读。现在是这样的:myformnow

<?php
#this does the exact same thing as NULL, this lets the user know to fill out their first name
if(($_POST['Month'] == "Month") && ($_SERVER['REQUEST_METHOD'] == 'POST')){
    echo "<strong>Please select a month! &nbsp</strong>";
}


if(($_POST['Day'] == "Day") && ($_SERVER['REQUEST_METHOD'] == 'POST')){
    echo "<strong>Please select a day! &nbsp</strong>";
}


if(($_POST['Year'] == "Year") && ($_SERVER['REQUEST_METHOD'] == 'POST')){
    echo "<strong>Please select a year! &nbsp</strong>";
}
?>
<br />
Date of Birth: 
<select name='Month'>
<?php
#this code makes the it so when the user selects a month and submit the form and they forget something their selection will still be selected
$months = array(0 => 'Month', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

foreach($months as $monthsel) {
    if($_POST['Month'] == $monthsel) {
            $isselected = "selected";
    } else {
            $isselected = "";   
    }

echo "<option value='$monthsel' $isselected>$monthsel</option> \n";
}

?>
</select>



<select name='Day'>
<?php
#this code makes the it so when the user selects a month and submit the form and they forget something their selection will still be selected
$day = array(0 => 'Day', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31');

foreach($day as $daysel) {
    if($_POST['Day'] == $daysel) {
            $isselected = "selected";
    } else {
            $isselected = "";   
    }

echo "<option value='$daysel' $isselected>$daysel</option> \n";
}

echo "\n";
?>
</select>

<select name='Year'>
<?php
#this code makes the it so when the user selects a month and submit the form and they forget something their selection will still be selected
$year = array(0 => 'Year', '2019', '2018', '2017', '2016', '2015', '2014', '2013', '2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005', '2004', '2003', '2002', '2001', '2000', '1999', '1998', '1997', '1996', '1995', '1994', '1993', '1992', '1991', '1990', '1989', '1988', '1987', '1986', '1985', '1984', '1983', '1982', '1981', '1980', '1979', '1978', '1977', '1976', '1975', '1974', '1973', '1972', '1971', '1970', '1969', '1968', '1967', '1966', '1965', '1964', '1963', '1962', '1961', '1960', '1959', '1958', '1957', '1956', '1955', '1954', '1953', '1952', '1951', '1950', '1949', '1948', '1947', '1946', '1945', '1944', '1943', '1942', '1941', '1940', '1939', '1938', '1937', '1936', '1935', '1934', '1933', '1932', '1931', '1930', '1929', '1928', '1927', '1926', '1925', '1924', '1923', '1922', '1921', '1920', '1919', '1918', '1917', '1916', '1915', '1914', '1913', '1912', '1911', '1910');

foreach($year as $yearsel) {
    if($_POST['Year'] == $yearsel) {
            $isselected = "selected";
    } else {
            $isselected = "";   
    }

echo "<option value='$yearsel' $isselected>$yearsel</option> \n";
}

echo "\n";
?>

我尝试查看if,elseif,else语句。但是,每次尝试执行此操作时,都会给代码一个错误,指出我做错了什么。所以我不确定该怎么做。

echo "\n";

我希望月,日,年保持动态,并得出问题摘要中所述的结果。

1 个答案:

答案 0 :(得分:0)

您可以在构建消息之前存储丢失的位,然后确定有多少部分。如果有错误,则只有一个错误-第一个错误是由自己解决的,否则,这些错误将用array_pop()组合成一个列表,最后一个错误将用and删除并添加文本中的$errors = []; if(($_POST['Month'] == "Month") && ($_SERVER['REQUEST_METHOD'] == 'POST')){ $errors[] = "month"; } if(($_POST['Day'] == "Day") && ($_SERVER['REQUEST_METHOD'] == 'POST')){ $errors[] = "day"; } if(($_POST['Year'] == "Year") && ($_SERVER['REQUEST_METHOD'] == 'POST')){ $errors[] = "year"; } if ( !empty ( $errors ) ) { $msg = "<strong>Please select a "; if ( count($errors) == 1 ) { $msg .= $errors[0]; } else { $lastError = array_pop($errors); $msg .= implode(", ", $errors). " and " .$lastError; } $msg .= "! &nbsp</strong>"; echo $msg; } ...

job('example') {
    triggers {
        bitbucketPush()
    }
}