gpt4 book ai didi

php - 如何制作动态月、日、年错误信息

转载 作者:行者123 更新时间:2023-11-28 00:23:45 26 4
gpt4 key购买 nike

我想让我的表单的月、日、年错误文本动态化,如果用户不选择月和日,它会说“请选择年份”。如果用户没有选择所有三个,它会说“请选择月、日和年”。如果用户不选择年份,它会说“请选择年份”等等……目前,我的代码只是说“请选择月份”“请选择日期”“请选择年份”并排,我认为我应该解决这个问题,以便用户更容易阅读。这是它现在的样子: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";

我希望月、日、年是动态的,并得到问题摘要中所述的结果。

最佳答案

您可以在构建消息之前存储丢失的位,然后决定有多少部分。如果有错误,如果只有一个错误 - 第一个错误被自己排除,否则错误被构建到一个列表中 implode() 并用 删除最后一个错误array_pop() 并在文中添加了...

$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;
}

关于php - 如何制作动态月、日、年错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54751952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com