Can someone help me figure out how to prompt a user to enter their birth day (da
ID: 3816279 • Letter: C
Question
Can someone help me figure out how to prompt a user to enter their birth day (day, month and year). When the user enters the date, the information should be processed by embedded PHP code and the user's age is displayed. If it is the user’s birthday, when the page is run, then a special Happy Birthday greeting should be included with the display of the age. It is supposed to include 1 form and a PHP code to process the form info. It is supposed to have a function that processes the form info and a function call to the created functions.
This is what I have so far and Im not sure how to do the rest. :-/ Can someone help guide me through it?
<!DOCTYPE html>
<html>
<head>
<title>Birthday</title>
</head>
<body>
<form name = "form1" method="POST" action ="">
<label> Please Enter your birthday</label><br>
<input type="text" name="birthday" VALUE =""><br><br>
<input type="submit" name="b1" VALUE = "Submit">
</form>
</body>
</html>
Explanation / Answer
form.html
We create three input texts for dd, mm, and yyyy.
<html>
<head>
<title>Birthday</title>
</head>
<body>
<form name="birthday" action ="postData.php" method="post">
<label> Please enter your birthday(dd-mm-yyyy) :</label>
<input type="text" name="dd" size="1" min="1" max="31" maxlength="2">
-<input type="text" name="mm" size="1" min="1" max="12" maxlength="2">
-<input type="text" name="yyyy" size="2" min="1900" max="2017" maxlength="4">
<input type="submit" name= "formSubmit" value = "Submit">
</form>
</body>
</html>
To process the data from above form, we have specified in action of form "postData.php".
postData.php
<?php
$day = $_POST["dd"];
$month = $_POST["mm"];
$year = $_POST["yyyy"];
$dob = $day."-".$month."-".$year;
$today = date("Y-m-d");
$todayDay = date("j");
$todayMonth = date("n");
$diff = date_diff(date_create($dob), date_create($today));
echo 'Your Age is '.$diff->format('%y');
if ($todayDay ==$day && $todayMonth== $month)
echo 'Wish you a very happy birthday';
?>
First we save the data from form using $_POST.
Then we calculate the age by calculating difference between present date and provided date of birth.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.