简体   繁体   中英

How can you make drop down lists for months, dates, and years?

I'm having difficulties working on a project for school. Here is what I have to do::

Assignment: Birthday Math

Part I

Modify your Math adding program to ask the user to enter 2 numbers AND to choose either addition, subtraction, multiplication OR division.

They must also enter a guess at the correct answer.

You must also deal with the possibility of the user trying to divide by zero...

On the page that processes the form compare their guess with the actual answer and tell them if they are right or wrong. Give them user friendly, fun, creative, polite, helpful feedback. Make sure that you display back ALL of the form information you collected from them.

Hint: Use a switch statement if you want, or just try nested if/else statements.

Part II

Also ask them to enter their birthdate.

Use three drop-down lists in your form to allow the user to select the month, date and year of their birth date. An array should be used for the month and each of these lists will require the use of a loop to place the values into a drop down list.

If a user goes to your site on their birth date, they should see a 'special message'.

Criteria

user friendly, fun and creative

all pages structured appropriately and use appropriate HTML tags and validate HTML5 with strict XHTML syntax. Use HTML LINT Checker to check for syntax errors.

forms are validated (checked to see if they are empty)

any page that contains forms uses PHP_SELF and contains Sticky forms

form fields are accessible and use fieldset where necessary

each of the scripts function with no PHP errors or warning and work properly

all PHP/HTML pages contain comments where appropriate and utilize 'white space' to make code easy to read (this include the 'view source' for php pages).

So, I already have the math and radio buttons finished, but the part where i am struggling is the birthday part.

<!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>

Now there must be a more simply way of putting in all the years, days, months, etc, potentially using arrays... but I just can't figure it out.

With usage of a for -loop for numeric values, and a foreach -loop for the months (based on an array of all months), you can easily display this.

  • A foreach -loop will loop through all elements of the array you call it on, and you can use this to echo them out (or process them, but you only need to echo them here).
  • A for -loop will loop through numeric values, for instance from 1-31, as you need for your assignment. You choose a start and end-point, and the increment for each time the loop finishes (for your assignment, this is 1).

A clever little feature here is to use the date() function to call the current year, so your script is always including the current year, without having to manually update the script every year.

An example is given below.

<?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>';
?>

A quick comment about the usage of $_SERVER['PHP_SELF'] : You should be careful about this, and should at least escape it, as it can be exploited. Take a look at w3schools.com talking about form validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM