Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/****HTML***/ <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http:

ID: 3871092 • Letter: #

Question

/****HTML***/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>P6: Handling User Input - Scholarship Form</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CTP 130 Participation 6 Assignment."/>
<meta name="keywords" content="CTP 130, assignment 6, PHP coding, book exercises"/>
<meta name="author" content="Jenn Bailey"/>
</head>
<body>
<h2>P6: Handling User Input - Scholarship Form</h2>
<form name = "scholarship" action = "process_Scholarship.php" method = "post">
   <p>First Name: <input type="text" name="fName" /></p>
   <p>Last Name: <input type="text" name="lName" /></p>
   <p><input type="submit" name="Submit" value="Send Form" /></p>
</form>

</body>
</html>

/****PHP***/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>P6: Handling User Input - Scholarship Form</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CTP 130 Participation 6 Assignment."/>
<meta name="keywords" content="CTP 130, assignment 6, PHP coding, book exercises"/>
<meta name="author" content="Jenn Bailey"/>
</head>
<body>
<?php

   $firstName = validateInput($_POST['fName'], "First name");
   $lastName = validateInput($_POST['lName'], "Last name");
  
   //display error message
   if($errorCount > 0) {
       echo "Please re-enter the information below<br/>";
       redisplayForm($firstName, $lastName);
   }
  
   //display missing fields
   function displayRequired($fieldName) {
       echo "The field "$fieldName" is required.<br/>";
   }
  
   //validate the form input
   function validateInput($data, $fieldName) {
       global $errorCount;
       if(empty($data)) {
           displayRequired($fieldName);
           ++$errorCount;
           $retval = " ";
       } else { //Send an email by replacing the "recipient@example.edu" with your address
           $To = "recipient@mail.edu";
           $Subject = "Scholarship Form Results";
           $Message = "Student Name: ".$firstName." ".$lastName;
           $result = mail($To, $Subject, $Message);
           if($result) {
               $resultMsg = "Your message was successfully sent.";
           } else {
               $resultMsg = "There was a problem sending your message.";
           }      
           ?>
               <h2>Scholarship Form</h2>
               <p>Thank you for filling our the scholarship form<?php
                   if(!empty($firstName))
                       echo ", $firstName"
                   ?>. <?php echo $resultMsg;?>
           <?php
       }
       return($retval);
   }
   $errorCount = 0;
      
   //display form again if there are missing fields
   function redisplayForm($firstName, $lastName) {
       ?>
       <h2>Scholarship Form</h2>
       <form name="scholarship" action="process_Scholarship.php" method="post">
           <p>First Name: <input type="text" name="fName" value="<?php echo $firstName; ?>"/></p>
           <p>Last Name: <input type="text" name="lName" value="<?php echo $lastName; ?>"/></p>
           <p><input type="reset" value="Clear Form"/>&nbsp;
           &nbsp;<input type="submit" name="Submit" value="Send Form" /></p>
       </form>
       <?php
   }

?>
</body>
</html>

Scholarship Form Thank you for filling our the scholarship form.. Your message was successfully sent. Scholarship Form Thank you for filling our the scholarship form.. Your message was successfully sent.

Explanation / Answer

They remain in the fields because you are explicitly telling PHP to fill the form with the submitted data.

(As shown in the underlined code)

<form name="scholarship" action="process_Scholarship.php" method="post">
           <p>First Name: <input type="text" name="fName" value="<?php echo $firstName; ?>"/></p>
           <p>Last Name: <input type="text" name="lName" value="<?php echo $lastName; ?>"/></p>

remove those lines or if you want to use a condition then do a if statement for echo and clear the post data.

$_POST=array();

or re-direct the form to another page on submission.