Created
December 20, 2013 07:02
-
-
Save itskingori/8051350 to your computer and use it in GitHub Desktop.
User Input Through Forms + HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Exercise 12: Forms + HTML</title> | |
</head> | |
<body> | |
<?php | |
// For this PHP exercise, first create an array called $months. Use the | |
// names of the months as keys, and the number of days for each month as | |
// values. For February, use the following for your value: "28 days, if leap | |
// year 29". | |
// Next, write a function to create an option element for a form's select | |
// field. Make sure each option will be upper case. Both the array and the | |
// function should precede the HTML for the page. | |
// Once again, you will be requesting user input. Create a form for the user | |
// with the request, “Please choose a month”. | |
// Next, provide a select field with the months as options, looping through the | |
// array you created and using the function to create the option elements. When | |
// the user clicks the submit button, return the statement ... | |
// “The month of $month has $number days.” | |
// Where $month is the name of the month the user chose, and $number is the | |
// number of days. Be sure to include a different response for February. | |
$months = array("january" => 31, | |
"february" => "28 days, if leap year 29", | |
"march" => 30, | |
"april" => 30, | |
"may" => 30, | |
"june" => 30, | |
"july" => 30, | |
"august" => 30, | |
"september" => 30, | |
"october" => 30, | |
"november" => 30, | |
"december" => 30 | |
); | |
// Define function to create single option tag for the select | |
function option($month){ | |
echo '<option value="'.$month.'">'.ucfirst($month).'</option>'; | |
} | |
?> | |
<form action="" method="GET"> | |
<p>Please choose a month.</p> | |
<!-- Dropdown select with the month generated using PHP from --> | |
<select name="month"> | |
<?php | |
//Create options using the array and the function. | |
foreach ($months as $k => $v){ | |
option($k); | |
} | |
?> | |
</select> | |
<!-- Submit button --> | |
<input type="submit" value="Submit"> | |
</form> | |
<p> | |
<?php | |
if (!empty($_GET['month'])) { | |
//Get the month | |
$month = $_GET['month']; | |
// Create the output | |
echo "The month of $month has ".$months[$month]." days."; | |
}; | |
?> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment