-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_scholarship.php
64 lines (58 loc) · 2.39 KB
/
process_scholarship.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!-- PHP Chapter 4 example exercise -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scholarship Form</title>
</head>
<body>
<?php
// definition of the DisplayRequired() function
function displayRequired($fieldName) {
echo "<p style='color:red;'>The Field \"$fieldName\" is required!<p/>";
}
// definition of validateInput() function
function validateInput($data, $fieldName) {
global $errorCount;
if(empty($data)) {
// this is when the form field is empty
displayRequired($fieldName);
++$errorCount;
$retval = "";
} else {
// clean up the input when it is not empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return $retval;
} // end of function
// definition of the redisplayForm() function
function redisplayForm($firstName, $lastName) {
?>
<h2 style ="text-align: center;">Scholarship Form</h2>
<form name="scholarship" action="process_scholarship.php" method="post">
<label for="fName">First Name:</label>
<input type="text" name="fName" id="fName" value="<?php echo $firstName;?>"/>
<br/>
<br/>
<label for="lName">Last Name:</label>
<input type="text" name="lName" id="lName" value="<?php echo $lastName;?>"/>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$errorCount = 0;
// Access data from the $_POST autoglobal array
$firstName = validateInput($_POST["fName"], "First Name");
$lastName = validateInput($_POST["lName"], "Last Name");
// Either output the confirmation or explanation
if($errorCount > 0) {
echo "<p>Please re-enter the missing information below.</p>";
redisplayForm($firstName, $lastName);
} else {
echo "Thank you for filling out the scholarship form, $firstName $lastName!";
}
?>
</body>
</html>