Web Development Exercise 4-2

User Generated

crgvgfba

Computer Science

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:
                          <?php
                          ?>


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.
                                                                       225
     function validateInput($data, $fieldName) {
          global $errorCount;
          if (empty($data)) {
               echo "\"$fieldName\" is a required field.\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 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
                    field.\n";
               ++$errorCount;
               $retval = "";
          } else { // Only clean up the input if it isn't
                   // empty
               $retval = trim($data);
               $retval = stripslashes($retval);
               $pattern = "/^[\w-]+(\.[\w-]+)*@" .
                    "[\w-]+(\.[\w-]+)*" .
                    "(\.[[a-z]]{2,})$/i";
               if (preg_match($pattern, $retval)==0) {
               echo "\"$fieldName\" is not a valid e-mail
                         address.\n";
                    ++$errorCount;
               }
          }
          return($retval);
     }
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;
            else
                 $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,
                        $Message);
      }
      else {
           $SenderAddress = "$Sender ";
           $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 "Your message has been sent. Thank you, "
                      . $Sender . ".\n";
           else
                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
      server.

10. Open ContactForm.php by entering the following URL:
    http:///PHP_Projects/Chapter.04/Projects/
    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
    errors.

11. Close your Web browser window.

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer


Anonymous
Just what I was looking for! Super helpful.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags