Web Development Exercise 4-2

Feb 1st, 2015
Computer Science
Price: $5 USD

Question description

In this exercise, you will create an All-in-One form that is a work-
                  ing “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.

                     1.   Create a new document in your text editor. Type the
                          !DOCTYPE declaration,  element, header information,
                          and  element. Use the strict DTD and “Contact Me” as
                          the content of the  element.

                     2.   Add the opening and closing tags for the PHP script section
                          in the body of the document:

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.\n";
               $retval = "";
          } else { // Only clean up the input if it isn't
                   // empty
               $retval = trim($data);
               $retval = stripslashes($retval);
4.   Add a function called validateEmail() immediately after the
     validateInput() function. This function is almost exactly
     like the validateInput() 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 Chapter 3.
     function validateEmail($data, $fieldName) {
          global $errorCount;
          if (empty($data)) {
               echo "\"$fieldName\" is a required
               $retval = "";
          } else { // Only clean up the input if it isn't
                   // empty
               $retval = trim($data);
               $retval = stripslashes($retval);
               $pattern = "/^[\w-]+(\.[\w-]+)*@" .
                    "[\w-]+(\.[\w-]+)*" .
               if (preg_match($pattern, $retval)==0) {
               echo "\"$fieldName\" is not a valid e-mail
5.   Add a function called displayForm() immediately after the
                        validateEmail() function. This function takes one param-
                        eter for each form field, and displays the form. It uses the
                        parameters for sticky form functionality.
                        function displayForm($Sender, $Email, $Subject,
                        $Message) {

                        Contact Me
                        Your Name: 
if ($errorCount==0)
                 $ShowForm = FALSE;
                 $ShowForm = TRUE;

 8.   Next, add a conditional statement that checks the value
      of $ShowForm. If $ShowForm is TRUE, the form is displayed.     227
      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 "Please re-enter the form
                      information below.\n";
           displayForm($Sender, $Email, $Subject,
      else {
           $SenderAddress = "$Sender ";
           $Headers = "From: $SenderAddress\nCC:
           // Substitute your own email address for
           // recipient@example.com
           $result = mail("recipient@example.com",
                            $Subject, $Message, $Headers);
           if ($result)
                echo "Your message has been sent. Thank you, "
                      . $Sender . ".\n";
                echo "There was an error sending your
                      message, " .
                      $Sender . ".\n";

 9.   Save the document as ContactForm.php in the Projects
      directory for Chapter 4 and upload the document to the Web

10. Open ContactForm.php by entering the following URL:
    ContactForm.php. Verify that the form validates the input
    fields correctly, redisplays the sticky form when there are
    errors, and sends the e-mail message when there are no

11. Close your Web browser window.

Tutor Answer

(Top Tutor) Daniel C.
School: UC Berkeley

Studypool has helped 1,244,100 students

Review from our student for this Answer

Feb 3rd, 2015
"awesome work thanks"
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1828 tutors are online

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