我在为学校项目工作时遇到了困难。这是我要做的事情::
作业:生日数学
第一部分
修改数学添加程序,要求用户输入2个数字 AND选择加法,减法,乘法或 分裂。
他们还必须猜出正确答案。
您还必须处理用户尝试分割的可能性 零...
在处理表单的页面上比较他们的猜测 实际答案并告诉他们是对还是错。给他们用户 友好,有趣,有创意,有礼貌,乐于助人的反馈。请确保你 显示您从中收集的所有表单信息。
提示:如果需要,可以使用switch语句,或者只是尝试嵌套if / else 语句。
第二部分
还要求他们输入他们的生日。
在表单中使用三个下拉列表以允许用户选择 他们出生日期的月份,日期和年份。应该使用一个数组 对于月份而言,这些列表中的每一个都需要使用循环 将值放入下拉列表中。
如果用户在出生日期前往您的网站,他们应该看到一个 '特别留言'。
标准
用户友好,有趣和富有创意
所有页面都经过适当的结构化并使用适当的HTML标签和 使用严格的XHTML语法验证HTML5。使用HTML LINT Checker 检查语法错误。
验证表单(检查它们是否为空)
任何包含表单的页面都使用PHP_SELF并包含粘性表单
可以访问表单字段并在必要时使用fieldset
每个脚本都运行,没有PHP错误或警告和工作 适当
所有PHP / HTML页面都包含适当和有用的注释 '白色空间'使代码易于阅读(这包括'视图 源'对于php页面。)
所以,我已经完成了数学和单选按钮,但我正在努力的部分是生日部分。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Howdy Stranger</title>
</head>
<body>
<h1>PHP_SELF</h1>
<?php
//process the form if the submit button was pressed
if (isset($_POST['submit'])) {
//form validation goes here
/////////////////////////////////////////VARIABLES ARE BEING MADE HERE
//simplify the form variables
//later on we will do this in form validation
//create a variable called firstname and store in it
//the value from the POST array for firstname from the form
$firstname = $_POST['firstname'];
//creating variables for num1 and num2 that user inputed
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
//creating a variable called guess and store it in the
//value from the POST array for the guess from the form
$guess = $_POST['guess'];
$month = $_POST['month'];
$year = $_POST['year'];
$day = $_POST['day'];
$date = $year ."-". $month ."-".$day;
$date = date("Y-m-d",strtotime($date));
if(date('m-d') == date('m-d', $date)) {
// today is users birthday. echo out "nice" message
echo "<p>Happy Birthday $firstname! You're nice and old now! Go have a good one!</p>\n";
} else {
echo "<p>Your birthday is on $date.</p>\n";
}
//creates the variable "sum"
//after user chooses certian "action" the sum will
//then be calculated using $num1 and $num2
if($_POST['action'] == "add") {
$sum = $num1 + $num2;
} else if($_POST['action'] == "subtract") {
$sum = $num1 - $num2;
} else if($_POST['action'] == "multiply") {
$sum = $num1 * $num2;
} else if($_POST['action'] == "divide") {
$sum = $num1 / $num2;
}
//echos out to the user and lets them know that they guessed correctly.
//also shows them their math
if ($sum == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess, using the numbers $num1 and $num2.<p>\n";
} else {
echo "<p>$firstname, you answered incorrectly. The correct answer is $sum.</p>\n";
}
} //end of the isset submit condional statement
//show the form if it is the user's first time her OR if any of the required forms are missing
if(!isset($_POST ['submit']) OR empty($firstname) OR empty($num1) OR empty($num2)) { ?>
<h2>Please fill out the following: </h2>
<!--FORM BEGINS-->
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="firstname">Please enter your first name: </label>
<input id="firstname" type="text" size="30" name="firstname" value="<?php if(isset($firstname)) echo $firstname; ?>"/><p>
<!--Challenge Dealio-->
<p><label for="Num1">Please enter a number: </label>
<input id="Num1" type="number" size="30" name="num1" value="<?php if(isset($num1)) echo $num1; ?>" /></p>
<p><label for="Num2">Please enter another number: </label>
<input id="Num2" type="number" size="30" name="num2" value="<?php if(isset($num2)) echo $num2; ?>"/><p>
<p>Please choose one of the following: </p>
<p>
<!--All radio buttons need the same "name" but with different values-->
<!--they each need their own special id too-->
<input name="action" id="add" type="radio" value="add" />Add<br />
<input name="action" id="subtract" type="radio" value="subtract" />Subtract<br />
<input name="action" id="multiply" type="radio" value="multiply" />Multiply<br />
<input name="action" id="divide" type="radio" value="divide" />Divide<br />
</p>
<p><label for="guess">Please put in a guess for the answer: </label>
<input id="guess" type="number" size="30" name="guess" value="<?php if(isset($guess)) echo $guess; ?>"/></p>
<!--option allows user to select the year they were born-->
<!--for simplicity sake, I am only doing from 1970-2015-->
<!--it would be way too many option values to make-->
<!--and would take up a lot of space in the code-->
<!--hope this works for you Charla-->
<select name="year" id="year">
<option value="1">1970</option>
<option value="2">1971</option>
<option value="3">1972</option>
<option value="4">1973</option>
<option value="5">1974</option>
<option value="6">1975</option>
<option value="7">1976</option>
<option value="8">1977</option>
<option value="9">1978</option>
<option value="10">1979</option>
<option value="11">1980</option>
<option value="12">1981</option>
<option value="13">1982</option>
<option value="14">1983</option>
<option value="15">1984</option>
<option value="16">1985</option>
<option value="17">1986</option>
<option value="18">1987</option>
<option value="19">1988</option>
<option value="20">1989</option>
<option value="21">1990</option>
<option value="22">1991</option>
<option value="23">1992</option>
<option value="24">1993</option>
<option value="25">1994</option>
<option value="26">1995</option>
<option value="27">1996</option>
<option value="28">1997</option>
<option value="29">1998</option>
<option value="30">1999</option>
<option value="31">2000</option>
<option value="32">2001</option>
<option value="33">2002</option>
<option value="34">2003</option>
<option value="35">2004</option>
<option value="36">2005</option>
<option value="37">2006</option>
<option value="38">2007</option>
<option value="39">2008</option>
<option value="40">2009</option>
<option value="41">2010</option>
<option value="42">2011</option>
<option value="43">2012</option>
<option value="44">2013</option>
<option value="45">2014</option>
<option value="46">2015</option>
</select>
<!--option allows user to select the month they were born.-->
<select name="month" id="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="day" id="day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</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="10">21</option>
<option value="11">21</option>
<option value="12">22</option>
<option value="13">23</option>
<option value="14">24</option>
<option value="15">25</option>
<option value="16">26</option>
<option value="17">27</option>
<option value="18">28</option>
<option value="19">29</option>
<option value="20">30</option>
<option value="31">31</option>
</select>
<!--Submit Button-->
<input type="submit" name="submit" value="Enter" />
</form>
<?php
} //end form conditional statement
?>
</body>
</html>
现在必须有一种更简单的方式来投入所有年,日,月等,可能使用数组......但我无法弄明白。
答案 0 :(得分:0)
使用for
循环表示数值,并使用foreach
循环表示月份(基于所有月份的数组),您可以轻松地显示它。
foreach
- 循环将循环遍历您调用它的数组的所有元素,并且您可以使用它来回显它们(或处理它们,但您只需在此处回显它们)。 for
- 循环将循环显示数值,例如1-31,如您分配所需。您选择一个起点和终点,以及每次循环结束时的增量(对于您的分配,这是1)。这里一个聪明的小功能是使用date()
函数来调用当前年份,因此您的脚本始终包含当前年份,而无需每年手动更新脚本。
下面给出一个例子。
<?php
$months = array("January", "February", "March", "Apil", "May", "June",
"July", "August", "September", "October", "November", "Descember");
$yearFrom = 1950; // The first year included in the drop-down for years
// $yearFrom = date("Y")-80;
// Using this line instead, gives a dynamic range of years, always 80 years
// Echo out all years via a for-loop
echo '<select name="year" id="year">';
for ($yearFrom; $yearFrom <= date("Y"); $yearFrom++) {
// Each $yearFrom represent a year, always incremented by 1
echo "<option value=\"$yearFrom\">$yearFrom</option>";
}
echo '</select>';
// Echo out all months from the array $months
echo '<select name="month" id="month">';
foreach($months as $key=>$value) {
// $key is the index of the array, starting at 0
$numericMonth = $key + 1;
echo "<option value=\"$numericMonth\">$value</option>";
}
echo '</select>';
// Echo out all days (1-31) via a for-loop
echo '<select name="day" id="day">';
for ($i=1; $i <= 31; $i++) {
// Each $i represents a numeric value of days, from 1-31
echo "<option value=\"$i\">$i</option>";
}
echo '</select>';
?>
关于$_SERVER['PHP_SELF']
用法的快速评论:你应该小心这一点,至少应该逃避它,因为它可以被利用。看看w3schools.com谈论表单验证。