This was built for a university assignment that I needed to do for a web programming subject. The aim was to make a registration form that would be sitting on a fitness club website. The spec sheet stated that the form and validation code (must be php) be contained in the same file, on validation the information must be outputed to a text file and redirected to a thank you page.

The PHP validation Code

This code was placed above the start of the actual markup for the page.

<?php

if ($_POST['submit']) {

 // Variables
 $firstName = $_POST['firstN'];
 $secondName = $_POST['secondN'];
 $dob = $_POST['dob'];
 $email = $_POST['email'];
 $address = $_POST['address'];
 $membershipType = $_POST['membershipType'];
 $creditCardNumber = $_POST['creditCardNumber'];
 $creditCardExpiryM = $_POST['creditCardExpiryM'];
 $creditCardExpiryY = $_POST['creditCardExpiryY'];
 $membershipDuration = $_POST['membershipDuration'];
 $count = 0;

 // Form Validation
 if (empty($_POST['firstName'])) {
 echo "<p>Please enter your first name!</p>";
 } else {
 $firstName = $_POST['firstName'];
 $count++;
 }

if (empty($_POST['secondName'])) {
 echo "<p>Please enter your second name!</p>";
 } else {
 $secondName = $_POST['secondName'];
 $count++;
 }

 if (empty($_POST['dob'])) {
 // Do nothing
 } else {
 $dob = $_POST['dob'];
 }

 if (empty($_POST['gender'])) {
 // Do nothing
 } else {
 $gender = $_POST['gender'];
 }

 if (empty($_POST['email'])) {
 echo "<p>please enter an email address</p>";
 } else {
 if (isset($_POST['email']) == true && empty($_POST['email']) == false) {
 $email = $_POST['email'];
 if (filter_var($email, FILTER_VALIDATE_EMAIL) == true) {
 $count++;
 } else {
 echo "<p>Please enter a valid email</p>";
 }
 }
 }

 if (empty($_POST['address'])) {
 // Do nothing
 } else {
 $address = $_POST['address'];
 }

 if ($_POST['membershipType'] == 'null') {
 echo "<p>Please select a membership type</p>";
 } else {
 $membershipType = $_POST['membershipType'];
 $count++;
 }

 if (empty($_POST['creditCardNumber'])) {
 echo "<p>Please enter your credit card number!</p>";
 } else {
 $creditCardNumber = $_POST['creditCardNumber'];
 $count++;
 }

 if (empty($_POST['creditCardExpiryM'])) {
 echo "<p>Please enter your credit card expiry month!</p>";
 } else {
 $creditCardExpiryM = $_POST['creditCardExpiryM'];
 $count++;
 }

 if (empty($_POST['creditCardExpiryY'])) {
 echo "<p>Please enter your credit card expiry year!</p>";
 } else {
 $creditCardExpiryY = $_POST['creditCardExpiryY'];
 $count++;
 }

 if ($_POST['membershipDuration'] == 'null') {
 echo "<p>Please select a membership duration!</p>";
 } else {
 $membershipDuration = $_POST['membershipDuration'];
 $count++;
 }

 // Create the string to write to file
 $outputstring = "n"
 .$firstName."t"
 .$secondName."t"
 .$dob."t"
 .$gender."t"
 .$email."t"
 .$address."t"
 .$membershipType."t"
 .$creditCardNumber."t"
 .$creditCardExpiryM."t"
 .$creditCardExpiryY."t"
 .$membershipDuration."t";

if ($count >= 8) {
 // Write to file
 $fp = fopen("users.txt", "a");
 fwrite($fp, $outputstring . PHP_EOL);
 fclose($fp);

 header("Location: thank-you.php");
 exit;
 }
}
?>

The Form HTML

<!-- Registration Form -->
 <form action="register.php" method="post">
 <!-- First Name -->
 <div class="rego_first_name">
 <span class="rego_label">First Name:*</span>
 <input name="firstName" type="text" value="<?php $firstName;?>" id="firstname" /><br />
 </div><!-- First Name Ends -->

 <!-- Second Name -->
 <div class="rego_second_name">
 <span class="rego_label">Second Name:*</span>
 <input name="secondName" type="text" value="<?php $secondName;?>" id="secondname" />
 </div><!-- Second Name Ends -->

 <!-- DOB -->
 <div class="rego_dob">
 <span class="rego_label">Date Of Birth:</span>
 <input name="dob" type="text" value="<?php $dob;?>" id="dob" /><br />
 <br />
 </div>

 <!-- Gender -->
 <div class="rego_gender">
 <span class="rego_label">Gender: </span>
 <div id="rego_radio">
 <div class="rego_male">
 <input name="gender" type="radio" id="mgender" value="male" />
 <div id="rego_mgender_label">Male</div>
 </div>
 <!-- .rego_male ends -->
 <div class="rego_female">
 <input name="gender" type="radio" id="fgender" value="female" />
 <div id="rego_fgender_label">Female</div>
 </div>
 </div>
 </div><!-- Gender Ends -->

 <!-- Email -->
 <div class="rego_email">
 <span class="rego_label">Email:*</span>
 <input name="email" type="text" value="<?php $email;?>" id="rego_email" /><br />
 </div> <!-- Email Ends -->

 <!-- Address -->
 <div class="rego_address">
 <span class="rego_label">Address:</span>
 <textarea name="address" rows="5" cols="26" value="<?php $address;?>"></textarea><br/>
 </div><!-- Address Ends -->

 <!-- Membership Type -->
 <div class="rego_membership">
 <span class="rego_label">Membership Type:* </span>
 <select name="membershipType" id="membership">
 <option value="null" selected="selected">Choose a membership</option>
 <option value="bronze">Bronze</option>
 <option value="silver">Silver</option>
 <option value="gold">Gold</option>
 </select>
 </div><!-- Membership Type Ends -->

 <!-- Credit Card Number -->
 <div class="rego_ccn">
 <span class="rego_label">Credit Card Number:* </span>
 <input name="creditCardNumber" type="text" value="<?php $creditCardNumber;?>" id="ccn" />
 </div><!-- Credit Card Number Ends -->

 <!-- Card Expiry Date -->
 <div class="rego_cced">
 <span class="rego_label">Credit Card Expiry Date:*</span>
 <input name="creditCardExpiryM" type="text" value="<?php $creditCardExpiryM;?>" id="creditCardExpiryM" />
 (Month) / <!-- Don't remove - seperator for expiry date -->
 <input name="creditCardExpiryY" type="text" value="<?php $creditCardExpiryY;?>" id="creditCardExpiryY" /> (Year)
 </div><!-- Card Expiry Date Ends -->

 <!-- Membership Duration -->
 <div class="rego_membership">
 <span class="rego_label">Duration of Membership:*</span>
 <select name="membershipDuration" id="duration">
 <option value="null" selected="selected">Choose a duration</option>
 <option value="Annual">Annual</option>
 <option value="5 Years">5 Years</option>
 <option value="10 Years">10 Years</option>
 <option value="Lifetime">Lifetime</option>
 </select>
 </div>
 <!-- Membership Duration Ends -->
 <!-- Submit -->
 <div>
 <input type="submit" name="submit" value="Submit" />
 </div>
 <!-- Submit Ends -->
 </form><!-- Registration Form Ends -->

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>