Martha's CIS 52 Lab #2 Number Exercise #3

Lab #2 Exercise #3.  Create a feedback form that accepts a user’s full name in one textbox (indicate a message that the user must enter his/her first name first, then last name), state abbreviation where he/she lives (such as CA, NY, etc.), an email address, and the month, day and year of his/her birthday.  Use case conversion functions to capitalize the first letter of each name the user submits.  Check that the user’s email address contains the @ symbol and print a warning otherwise.  If the user did not input a state abbreviation, assign a default value of “CA.”  Generate an error message if user did not input full name, email address or birthday. 
List all the values the user inputted splitting the full name into first name and last name in your output. 
Example of output:  First name:  John
                                Last name:  Smith
The order how you display the first name and last name in the output depends on the alphabetizing of the first name and last name.  For example, if the full name is Susan Abbitte.  Alphabetically, Abbitte comes before Susan.  So, the output for full name will be:
                                Last name:  Abbitte
                                First Name:  Susan
However, if the user enters Betty Magadan, then the output for full name will be:
                                First name:  Betty
                                Last name:  Magadan
Other items in the output:
a.     A message that tells the user how many days, hours, minutes and seconds until the big day (birthday) from today’s date.
b.    The length of user’s first name and the length of user’s last name.
c.    Shuffle all the characters in the last name and output the result.
d.    Output the last two characters in the lastname.
e.    If the user’s first name has a letter e or E, replace that with the letter o or O, respectively.  Output the result.
Reference:  http://www.w3schools.com/PHP/php_ref_string.asp

**************************************************************

CODES:

<?php
// Declare the variables
$name = $_POST['name'];
$state = $_POST['state'];
$email = $_POST['email'];
$birth = $_POST['birth'];

$name = ucWords($name);

function Validate_State($state) {
if ($state == false ||
$state == 0 ||
$state == "" ) {
$state == "CA";
}
else ($state == $state);
return $state;
}
$state = Validate_State($state);
echo "Your name is $name, and you live in the state of $state.<br />";
$fullName = explode(" ", $name);
// echo $fullName;
$fname=$fullName[0];
$lname=$fullName[1];
$sortName = array($fname, $lname);
sort($sortName);
if ($lname < $fname) {
echo "Last Name: ".$lname;
echo "<br />First Name: ".$fname;
} else {echo "First Name: ".$fname;
echo "<br />Last Name: ".$lname;
}
$number = strlen($lname);
echo "<br />Your last name has $number letters in it.";
echo "<br />Your first name has ".mb_strlen($fname)." letters in it.";
$chunk = substr($lname, (strlen($lname) - 2));
echo "<br />The last two letters of your last name are: ".$chunk;
echo "<br />Your last name shuffled is: ".str_shuffle($lname);
echo "<br />";

function Verify_Email_Address($email_address)
{
//Assumes that valid email addresses consist of user_name@domain.tld
$email_address = $email;
$at = strpos($email_address, "@");
$dot = strrpos($email_address, ".");

if($at === false ||
$dot === false ||
$dot <= $at + 1 ||
$dot == 0 ||
$dot == strlen($email_address) - 1) {
// return(false);
echo "<br />You must have a valid email address.<br />";
} else {
$user_name = substr($email_address, 0, $at);
$domain_name = substr($email_address, $at + 1, strlen($email_address));
echo "Your domain is ".$domain_name." and your user name is ".$user_name.".";
echo "Your full email address is: ".$email_address;
}
}
Verify_email_address($email_address);

function checkBirth($date) {
global $birth;
if ($birth == false ||
$birth == "0" ||
$birth == "") {
echo "You must include a birth date.<br />";
} else {
echo "<br />Your birth date is ".$birth.".<br />";
echo "It is ".getDays($date)." UNIX hours until your birthday.<br />";
}
}

checkBirth($date);
/* http://www.phpbuilder.com/board/showthread.php?t=10316456
$date = '01/24/2006';
echo date('Y-m-d', strtotime($date));
*/
// outputs 2006-01-24

function getDays($date) {
$bday = strtotime($birth);
$today = date(m.d.y);
$today = strtotime($today);
$thisYrBday = x; // I don't know how to extract the current year info
$nextBday = x + 365;
$daysTillBday = $nextBday - $today;
$daysTillBday = date($daysTillBday);
return $daysTillBday;
}
?>

 

 

**************************************************************

ANSWER:

Your name is , and you live in the state of .
First Name:
Last Name:
Your last name has 0 letters in it.
Your first name has 0 letters in it.
The last two letters of your last name are:
Your last name shuffled is:

You must have a valid email address.
You must include a birth date.

 

**************************************************************

 

 

RESOURCES:

http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html
http://www.tutorialhelpdesk.com/tutorials/web-development/php/date-and-time/calculating-the-difference-between-two-dates.html
http://www.hawkee.com/snippet/446/
http://www.php.net/manual/en/function.date.php
http://www.w3schools.com/php/php_ref_date.asp