我有一个程序,当您提交您的姓名和出生日期,月份和年份时,它会计算您的星座。我想要它,这样如果你不提交一条信息并看到错误信息,当你点击回来时你的答案仍然存在(粘性)。我已经看了一些其他线程,但在我的具体情况下无法实现它。谢谢!
这是我的html表单:
<!DOCTYPE HTML>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' rel='stylesheet' type='text/css'>
<title>
Zodiac
</title>
<style>
body{
background-image: url('space.jpg');
background-color: Blue;
background-size: 100%;
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 150%;
}
form{
background-color: LightBlue;
border: solid black 3px;
width: 35%;
}
</style>
</head>
<body>
<center>
<br>
<br>
<form action="zodiacformHandler.php" method="post">
<br>
<h1>What's your sign?</h1>
<br>
Full Name: <input type="text" name="fullName"><br>
<br>
Month:
<!-- Month dropdown -->
<select name="month" size="1">
<option disabled selected> </option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
Day:
<!-- Day dropdown -->
<select name="day" size="1">
<option disabled selected> </option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">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>
Year:
<!-- Year dropdown -->
<select name="year" size="1">
<option disabled selected> </option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
</select>
<input type="submit">
<br><br>
</form>
</center>
</body>
</html>
这是我的php formHandler:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Zodiac Form Handler </title>
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' rel='stylesheet' type='text/css'>
<style>
body{
background-image: url('space.jpg');
background-color: LightBlue;
background-size: 100%;
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 150%;
}
#forminfo{
padding: 25px;
}
img.signs{
height: 50%;
}
</style>
</head>
<body>
<table id="forminfo" align="center" BGCOLOR="#c6e9ee" style="border:3px solid black">
<tr><td>
<br>
<center>
<img class="signs" src="signs.png" align="center" /></a>
<h1>Your Astrological Sign:</h1>
</center>
<br>
<center>
<?php
$fullName = $_POST['fullName'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$formComplete = True; //boolean variable for if the form is valid or invalid
//checks to see if name is filled out.
if(empty($_POST['fullName'])){
echo "<p class='error'>ERROR: You didn't enter your name. Please go back and try again.</p>";
$formComplete = False;
}
//checks to see if the month is filled out.
if(empty($_POST['month'])){
echo "<p class='error'>ERROR: You didn't enter a month. Please go back and try again.</p>";
$formComplete = False;
}
//checks to see if dislikes is filled out.
if(empty($_POST['day'])){
echo "<p class='error'>ERROR: You didn't enter a day. Please go back and try again.</p>";
$formComplete = False;
}
//checks to see if a year is entered.
if(empty($_POST['year'])){
echo "<p class='error'>ERROR: You didn't enter a year. Please go back and try again.</p>";
$formComplete = False;
}
if ($formComplete){
function zodiac ( $month, $day, $year)
{
$zodiac = "";
if ( ( $month == 3 && $day > 20 ) || ( $month == 4 && $day < 20 ) ) { $zodiac = "Aries"; }
elseif ( ( $month == 4 && $day > 19 ) || ( $month == 5 && $day < 21 ) ) { $zodiac = "Taurus"; }
elseif ( ( $month == 5 && $day > 20 ) || ( $month == 6 && $day < 21 ) ) { $zodiac = "Gemini"; }
elseif ( ( $month == 6 && $day > 20 ) || ( $month == 7 && $day < 23 ) ) { $zodiac = "Cancer"; }
elseif ( ( $month == 7 && $day > 22 ) || ( $month == 8 && $day < 23 ) ) { $zodiac = "Leo"; }
elseif ( ( $month == 8 && $day > 22 ) || ( $month == 9 && $day < 23 ) ) { $zodiac = "Virgo"; }
elseif ( ( $month == 9 && $day > 22 ) || ( $month == 10 && $day < 23 ) ) { $zodiac = "Libra"; }
elseif ( ( $month == 10 && $day > 22 ) || ( $month == 11 && $day < 22 ) ) { $zodiac = "Scorpio"; }
elseif ( ( $month == 11 && $day > 21 ) || ( $month == 12 && $day < 22 ) ) { $zodiac = "Sagittarius"; }
elseif ( ( $month == 12 && $day > 21 ) || ( $month == 1 && $day < 20 ) ) { $zodiac = "Capricorn"; }
elseif ( ( $month == 1 && $day > 19 ) || ( $month == 2 && $day < 19 ) ) { $zodiac = "Aquarius"; }
elseif ( ( $month == 2 && $day > 18 ) || ( $month == 3 && $day < 21 ) ) { $zodiac = "Pisces"; }
return $zodiac;
}
echo ("Hello $fullName! Your zodiac sign is: ");
echo zodiac ( $month, $day, $year);
}
?>
<br><br>
<a href="zodiac.html" >Choose another date?</a>
<center>
<br>
</td></tr>
</table>
</body>
</html>
答案 0 :(得分:0)
我怀疑你已经看过使用这种方法的方法:
<input name="someField" type="text" value="<?php echo $_POST['someField']; ?>" />
由于SELECT字段的工作方式完全相同,因为您必须使用以下方法选择正确的值,因此正在努力奋斗:
<option selected="selected">someValue</option>
最好的办法可能是&#34; build&#34; PHP中的整个选择字段
例如
// Validate input
if($_POST['month'] != 3) //(Or some other validation check)
{
//Set the default month as January
$defaultMonth = 01;
}
else
{
// Input is valid, leave it as whatever the user set last time
$defaultMonth = $_POST['month']; //Don't do this without cleaning your input
}
// Define the different options to use in the select field
$months = array(
01 => 'January',
02 => 'February',
03 => 'March',
04 => 'April',
......
12 => 'December'
);
// "Create" the select field from the array above
echo '<select name="month" size="1">';
foreach($months as $monthNo => $desc)
{
// Decide if we want to put the selected="selected" line in
if($monthNo == $defaultMonth)
{
$selectedText = "selected='selected' ";
}
else
{
$selectedText = "";
}
echo "<option value='$monthNo' $selectedText>$desc</option>";
}
echo '</select>';
这允许您构建表单并输入所选的=&#39;选择&#39;在正确的部分。您的验证可以是您需要的任何内容。