Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //registration.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>Server Side Validation</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <?php
- //1. initialize php variables to hold form data
- $fn = $ln = $email = $birth_yr = "";
- $fnErr = $lnErr = $email_err = $birth_err = $secret_message = ""; //initialize variables to hold error messages
- $valid = true; //assume data is valid until proven otherwise
- //2. Test if the page is being loaded in response to a form submission
- if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["submit"])) {
- //****ERROR TESTING - just to show difference in page loading - REMOVE AFTER TESTING!!!!****
- $secret_message = "The form was submitted!";
- // 3. Test individual form fields to validate data
- //testing first name
- if (empty($_POST["fname"])) { // 3.1 test if fname field was empty
- //set an error message for the element and change valid to false
- $fnErr = "First name is required";
- $valid = false;
- }
- // 3.2 Once field is NOT empty,retrieve data and assess further
- else {
- $fn = $_POST["fname"]; //retrieve fname data
- $fn = test_input($fn); //clean up the fname data - see function below (lines 51 - 56)
- // 3.3 test data type and format using regular expression - use same regex as in your client side validation
- if (!preg_match("/^[a-zA-Z'!-]{2,35}$/", $fn)) {
- $fnErr = "First Name may only contain letters or ' ! and -";
- $valid = false;
- }
- }
- //testing last name
- // 3. Test individual form fields to validate data
- if (empty($_POST["lname"])) { // 3.1 test if fname field was empty
- //set an error message for the element and change valid to false
- $lnErr = "Last name is required";
- $valid = false;
- }
- // 3.2 Once field is NOT empty,retrieve data and assess further
- else {
- $ln = $_POST["lname"]; //retrieve fname data
- $ln = test_input($ln); //clean up the fname data - see function below (lines 51 - 56)
- // 3.3 test data type and format using regular expression - use same regex as in your client side validation
- if (!preg_match("/^[a-zA-Z'!-]{2,40}$/", $ln)) {
- $lnErr = "Last Name may only contain letters or ' ! and -";
- $valid = false;
- }
- }
- //testing email
- // 3. Test individual form fields to validate data
- if (empty($_POST["email"])) { // 3.1 test if fname field was empty
- //set an error message for the element and change valid to false
- $email_err = "Email is required";
- $valid = false;
- }
- // 3.2 Once field is NOT empty,retrieve data and assess further
- else {
- $email = $_POST["email"]; //retrieve fname data
- $email = test_input($email); //clean up the fname data - see function below (lines 51 - 56)
- // 3.3 test data type and format using regular expression - use same regex as in your client side validation
- if (!preg_match("/^[a-zA-Z0-9.]{2,30}@[a-zA-Z0-9.]{2,20}.[a-zA-Z]{2,4}$/", $email)) {
- $email_err = "Not a valid email.";
- $valid = false;
- }
- }
- //testing birth year
- // 3. Test individual form fields to validate data
- if (empty($_POST["birth_yr"])) { // 3.1 test if fname field was empty
- //set an error message for the element and change valid to false
- $birth_err = "Birth year is required.";
- $valid = false;
- }
- // 3.2 Once field is NOT empty,retrieve data and assess further
- else {
- $birth_yr = $_POST["birth_yr"]; //retrieve fname data
- $birth_yr = test_input($birth_yr); //clean up the fname data - see function below (lines 51 - 56)
- // 3.3 test data type and format using regular expression - use same regex as in your client side validation
- if (!preg_match("/^[0-9]{4}$/", $birth_yr)) {
- $birth_err = "Birth year must not contain any letters or special characters.";
- $valid = false;
- }
- }
- // 4.0 WE HAVE VALID DATA
- if ($valid) {
- // if we are successful in reaching here it means that all the form data has validated.
- //We can now choose to proceed to storing data in the database or sending an email etc.
- // today we will simply go to the thank you page
- header("Location: thankyou.php?fn=$fn");
- }
- } //end form validation block.
- /* This function uses three predefined PHP functions to clean up the data that has been retrieved from our form.
- trim() - Returns a string with leading and trailing spaces removed
- stripslashes() - Returns a string with backslashes stripped off
- htmlspecialchars() - converts some predefined characters to HTML entities
- */
- function test_input($data)
- {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <div class="container">
- <h3 >I am trying to validate this form on the client AND the server!</h3>
- <?php echo "<p>$secret_message</p>"; ?>
- <form
- id="reg_form"
- name="reg_form"
- method="post"
- action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
- onsubmit="return validate();"
- >
- <fieldset>
- <legend>Registration</legend>
- First Name: <input type="text" name="fname" id="fname" value="<?php echo $fn; ?>">
- <span id="fname_err" class="error"><?php echo $fnErr; ?></span>
- <br>
- Last Name: <input type="text" name="lname" id="lname" value="<?php echo $ln; ?>">
- <br>
- <span id="lname_err" class="error"><?php echo $lnErr; ?></span>
- <br>
- Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>">
- <span id="email_err" class="error"><?php echo $email_err; ?></span>
- <br>
- Birth Year:
- <input type="text" name="birth_yr" id="birth_yr" value="<?php echo $birth_yr; ?>">
- <span id="birth_err" class="error"><?php echo $birth_err; ?></span>
- <br><br>
- Gender: <input type="radio" name="gender" id="male" value="male"> Male
- <input type="radio" name="gender" id="female" value="female"> Female
- <br><br>
- <input type="submit" name="submit" value="Register">
- </fieldset>
- </form>
- </div>
- </body>
- </html>
- //thankyou.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>Learning PHP</title>
- <link rel="stylesheet" type="text/css" href="main.css" />
- </head>
- <body>
- <h2 >Welcome</h2>
- <!-- Pull the user's first and last names from the super global array and insert it into the welcome statement-->
- <p> <?php echo "$_GET[fn],"; ?> Thank you for registering with us</p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement