This is php class In this exercise, you will create a single PHP file to display
ID: 3701752 • Letter: T
Question
This is php class
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.
Explanation / Answer
If you have any doubts, please give me comment...
<!DOCTYPE html>
<html>
<head>
<title>Contact Me</title>
</head>
<body>
<?php
//checking whether given data is valid or not
function validateInput($data, $fieldName){
global $errorCount;
//checking whether data is empty or not
if(empty($data)){
//if data is empty display error message
echo ""$fieldName" is a required field.<br /> ";
++$errorCount;
$reval = "";
}
else{
//Only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripslashes($retval);
}
//returning clean data
return($retval);
}
//checking email valid or not
function validateEmail($data, $fieldName){
global $errorCount;
//checking whether data is empty or not
if(empty($data)){
//if data is empty display error message
echo ""$fieldName" is a required field.<br /> ";
++$errorCount;
$reval = "";
}
else{
//sanitizing email
$retval = filter_var($data, FILTER_SANITIZE_EMAIL);
//checking whehter email is valid or not
if(!filter_var($retval, FILTER_VALIDATE_EMAIL)){
echo ""$fieldName" is not a valid e-mail address.<br /> ";
}
}
//returning clean data
return($retval);
}
//display contact form
function displayForm($Sender, $Email, $Subject, $Message){
?>
<h2>Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<!-- For taking input name -->
<p>Your Name:
<input type="text" name="Sender" value="<?php echo $Sender;?>" /></p>
<!-- For taking input Email -->
<p>Your E-mail:
<input type="text" name="Email" value="<?php echo $Email;?>" /></p>
<!-- For taking input Subject -->
<p>Subject:
<input type="text" name="Subject" value="<?php echo $Subject;?>" /></p> <!-- For taking input Message -->
<p>Message:
<input type="text" name="Message" value="<?php echo $Message;?>" /></p>
<!-- Reset Form -->
<p><input type="reset" value="Clear Form" />
<!-- Submit form -->
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
//check whether form submit or not...
if(isset($_POST['Submit'])){
$Sender = validateInput($_POST['Sender'], "Your Name");
$Email = validateEmail($_POST['Email'], "Your E-mail");
$Sender = validateInput($_POST['Sender'], "Your Name");
$Message = validateInput($_POST['Message'], "Message");
//check whether errors are existed or not
if($errorCount ==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
//show form if errors exists
if($ShowForm==TRUE){
if($errorCount>0)
echo "<p>Please re-enter the form information below.</p> ";
displayForm($Sender, $Email, $Subject, $Message);
}
//if no errors send mail...
else{
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress CC: $SenderAddress ";
$result = mail("recipient@example.com", $Subject, $Message, $Headers);
if($result)
echo "<p>Your message has been sent. Thank you, ".$Sender.".</p> ";
else
echo "<p>There was 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.