In this exercise, you will create a single PHP file to display and process a for
ID: 3698966 • Letter: I
Question
In this exercise, you will create a single PHP file to display and process a form that is a working “Contact Me” page. This page will have inputs for the subject, the sender’s name, the sender’s e-mail address, and the message. The form will also send a copy of the message to the sender.
All the code required to make a functioning Contact Me PHP script is provided. You are required to type up all the code below. Then add comments into the code ( // or /* .... */) to fully explain it. Each function should be individually explained.
1. Create a new document in your text editor. Type the <!DOCTYPE HTML> declaration, <html> element, header information, and <body> element. The page title should be “Contact Me”.
2. Add the opening and closing tags for the PHP script section in the body of the document:
3. Add a function called validateInput() within the php tag. This function takes two parameters. The first parameter, $data, is a string to be validated. The second parameter, $fieldName, is the name of the form field. The function returns the $data parameter after it has been cleaned up.Notice that the function uses the global variable $errorCount.
4. Add a function called validateEmail() immediately after the validateInput() function. This function is almost exactly like the validateInput() function, but it adds a filter_var() (Links to an external site.)Links to an external site.test to validate that the entered e-mail address is in the correct format.
5. Add a function called displayForm() immediately after the validateEmail() function. This function takes one parameter for each form field and displays the form. It uses the parameters for sticky form functionality.
6. Immediately after the displayForm() function, declare and initialize a set of variables as follows:
7. Next, add the following code to check for and validate the input. Note that $_POST['Email'] is checked with the validateEmail() function instead of the validateInput() function.
8. Next, add a conditional statement that checks the value of $ShowForm. If $ShowForm is TRUE, the form is displayed. Otherwise, an e-mail message is sent and a status message is displayed. Note that a copy is sent to the sender.
9. Save the document as ContactForm.php.
10. Open ContactForm.php by entering the following URL: http://localhost/ContactForm.php.
Verify that the form validates the input fields correctly, redisplays the sticky form when there are errors, and attempts to send the e-mail message when there are no errors. (Not required to actually sent a validate email).
Explanation / Answer
here is your code : ----------->>>>>>>>>>
<!DOCTYPE html>
<html>
<head>
<title>Contact Me</title>
</head>
<body>
<?php
/* this function validate the input to be not empty
if the input is empty it will show an error message on the page and redisplay the page by
incrementing the errorCount variable*/
function validateInput($data,$feildName){
global $errorCount;
$retval = "";
if(empty($data)){
echo ""$feildName" is a required feild.<br /> ";
++$errorCount;
$retval = "";
}
else{
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
/*this function special validate the email to be non empty and it also checks the email format
this function also increment the errorCount variable to show its error
*/
function validateEmail($data,$feildName){
global $errorCount;
$retval = "";
if(empty($data)){
echo ""$feildName" is a required feild.<br /> ";
++$errorCount;
$retval = "";
}else{
$retval = filter_var($data,FILTER_SANITIZE_EMAIL);
if(!filter_var($retval,FILTER_VALIDATE_EMAIL)){
echo ""$feildName" is not a valid email.<br /> ";
++$errorCount;
$retval = "";
}
}
return($retval);
}
//this function will show the form as a html form
//it uses the tag of html to show the form on the page for input from the users
function dispalyForm($Sender,$Email,$Subject,$Message){ ?>
<h2>Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your Name :
<input type="text" name="Sender" value="<?php echo $Sender;?>" />
</p>
<p>Your E-mail :
<input type="text" name="Email" value="<?php echo $Email;?>" />
</p>
<p>Subject :
<input type="text" name="Subject" value="<?php echo $Subject;?>" />
</p>
<p>Message :
<input type="text" name="Message" value="<?php echo $Message;?>" />
</p>
<input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" />
</form>
<?php }
//here we declare some variable to track the state of the application
//showForm track to whether to show the form or send the message
//errorCount track whether there is an error in form feilds
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Subject = "";
$Email = "";
$Message = "";
//this will called every time you call the ContactForm.php
//it will check if the submit buton is pressed or not
//and if pressed it will validate every feild by calling the above
//validates function
//and also put appropriate value in showForm variable
if(isset($_POST['Submit'])){
$Sender = validateInput($_POST['Sender'],"Your Name");
$Email = validateEmail($_POST['Email'],"Your E-mail");
$Subject = validateInput($_POST['Subject'],"Subject");
$Message = validateInput($_POST['Message'],"Message");
if($errorCount == 0){
$ShowForm = FALSE;
}else{
$ShowForm = TRUE;
}
}
//here it will check the showForm variable and according to that
//will show the form for input or
//sends the message to the given email here
//and also display any error if occured during sending
if($ShowForm == TRUE){
if($errorCount > 0)
echo "<p>Please Re-Enter The Form Information Below </p>";
dispalyForm($Sender,$Email,$Subject,$Message);
}else{
$senderAddress = "$Sender <$Email>";
$Headers = "From: $senderAddress cc: $senderAddress ";
$result = mail("your email here ",$Subject,$Message,$Headers);
if($result){
echo "<p>your Message is Successfully Sent. Thank You</p>";
}else{
echo "<p> There is an error sending your message, ".$Sender." </p>";
}
}
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.