to create all in one form

Anonymous
timer Asked: Nov 9th, 2016

Question description

To create an All-in-One form that is a working “Contact Me” page. This page will have inputs for the subject, the sender’s name, the sender’s email address, and the message. The form will also send a copy of the message to the sender.

1. Create a new document in your text editor.

2. Use the following HTML document structure and add the opening and closing tags for the PHP script section in the body of the document:

<html>

<head>

</head>

<body>

<?php

?>

</body>

</html>

3. Add a function called validateInput (). 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.

function validateInput ($data, $fieldName) {

global $errorCount;

 if (empty($data)) {

echo "\"$fieldName\" is a required field.<br

/>\n";

++$errorCount;

$retval = "";

} else { // Only clean up the input if it isn't 
// empty

$retval = trim($data);

$retval = stripslashes($retval);

}

return ($retval) ;

}

4. Add a function called val idateEmail 0 immediately after the val i dateInput 0 function. This function is almost exactly like the val i dateInput function, but it adds a regular expression test to validate that the entered e-mail address is in the correct format. Note that the regular expression used is the same one introduced in Unit 3.

function validateEmail ($data, $fieldName)   {

global $errorCount;

if (empty($data))  {

echo "\"$fieldName\" is a required field.<br />\n";

++$errorCount;

$retval = "";

} else { // Only clean up the input if it isn't empty

$retval = trim($data);

  $retval = stripslashes($retval);

$pattern = "/A[\w-]+(\•[\w-]+)*@"  .

“[\w-]+(\. [\w-]+)*"  .

“(\.[[a-z]]{2,})$/I;

 if (preg_match($pattern, $retval)==0) {

echo "\"$fieldName\" is not a valid e-mail address .<br />\n";

++$errorCount;

}

}

return($retval);

}

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.

 function displayForm($Sender, $Email, $Subject, $Message) {

?>

<h2 style = "text-align:center">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; 7>" /></p>

 <p>Subject: <input type="text" name="Subject"

value="<?php

echo $Subject; ?>" /></p>

 <p>Message:<br />

 <textarea name="Message"><?php echo $Message;

?></textarea></p>

<p><input type="reset" value="Clear Form" />&nbsp;

&nbsp;<input type="submit" name="Submit"

value="Send Form" /></p>

</form>

<?php

}

6. Immediately after the displayForm() function, declare and initialize a set of variables as follows:

$ShowForm = TRUE;

 $errorCount = 0;

$Sender = "";

$Email = "";

$Subject = "";

$Message = "";

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.

 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;

}

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.

if ($ShowForm == TRUE)  {

 if ($errorCount>0) // if there were errors

 echo "<p>Please re-enter the form information below.</p>\n"; displayForm($Sender, $Email, $Subject, $Message);

}

else {

$SenderAddress = "$Sender <$Email>";

$Headers = "From: $SenderAddress\nCC:

$SenderAddress\n";

// Substitute your own email address for

// recipient@example.com

$result = mail("recipient@example.com",

$Subject, $Message, $Headers);

if ($result)

echo "10>Your message has been sent. Thank you, "

. $Sender . ".</p>\n";

else

echo "<p>There was an error sending your message, " . $Sender . ".</p>\n";

}

9. Save the document as ContactForm.php and upload the document to the Web server.


Tutor Answer

(Top Tutor) Studypool Tutor
School: UC Berkeley
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors