Advertisement
Andronicuszeno

registration.php

Apr 5th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.64 KB | None | 0 0
  1. <?php
  2.     // Create database connection
  3.     require_once("./includes/db_connection.php");
  4.     $db_connection = new mysqli($server, $user, $password, $dbname);
  5. ?>
  6. <?php
  7.     // The form submits to itself
  8.     require_once("./includes/general_functions.php");
  9.     require_once("./includes/validation_functions.php");
  10.     require_once("./includes/contact.php");
  11.    
  12.     $contact = new Contact();
  13.    
  14.     // Global variables
  15.     $errors = array();
  16.     $missing = array();
  17.     $fields_expected = array("firstname", "lastname", "address1", "address2", "city", "state", "zipcode", "phone", "email");
  18.     $fields_required = array("firstname", "lastname", "address1", "city", "state", "zipcode", "phone", "email", "captcha_result");
  19.     $fields_max_lengths = array("firstname" => 50, "lastname"  => 50, "address1" => 50, "address2"  => 50, "city" => 50, "state" => 50, "zipcode"  => 10, "phone" => 20, "email"  => 60);
  20.  
  21.     // Check to see if the form was submited
  22.     if (isset($_POST["submit"])) {
  23.         // Form was submitted.
  24.        
  25.         // Sanitize:
  26.         // Strip possible scripts from the form fields, and then trim for possible white spaces.
  27.         foreach($fields_expected as $field){
  28.             $contact->$field = trim(strip_tags($_POST[$field]));
  29.         }
  30.        
  31.         // Begin Validations:
  32.         $contact->gender = isset($_POST["gender"]) ? $_POST["gender"] : "";
  33.         $contact->accept = isset($_POST["accept"]) ? $_POST["accept"] : "";
  34.         if (!has_presence($contact->gender)){
  35.             $missing[] = "gender";
  36.         }
  37.         if (!has_presence($contact->accept)){
  38.             $missing[] = "accept";
  39.         }
  40.    
  41.         // Validate presence of required fields
  42.         foreach($fields_required as $field){
  43.             $value = trim($_POST[$field]); 
  44.             if (!has_presence($value)) {
  45.                 $missing[] = $field;   
  46.             }
  47.         }
  48.        
  49.         // Validate Maximum Length
  50.         foreach($fields_max_lengths as $field => $max) {
  51.             $value = trim($_POST[$field]);
  52.             if (!has_maximum_length($value, $max)) {
  53.                 $errors[$field] = ucfirst($field) . " is too long.";
  54.             }
  55.         }
  56.        
  57.         // Check Captcha
  58.         $captcha_result = trim(strip_tags($_POST["captcha_result"]));
  59.         $first_number = $_POST["first_number"];
  60.         $second_number = $_POST["second_number"];
  61.         $check_total = $first_number + $second_number;
  62.  
  63.         if ($captcha_result != $check_total) {
  64.             $errors["captcha"] = "The answer to the captcha was not correct, please try agian.";
  65.         }
  66.        
  67.        
  68.         // If there are no errors or any missing fields, then prepare form data for database insert.
  69.         if (empty($errors) && empty($missing)) {
  70.             // Insert record into the database
  71.            
  72.             // Escape all strings
  73.             $firstname = $db_connection->real_escape_string($contact->firstname);
  74.             $lastname = $db_connection->real_escape_string($contact->lastname);
  75.             $address1 = $db_connection->real_escape_string($contact->address1);
  76.             $address2 = $db_connection->real_escape_string($contact->address2);
  77.             $city = $db_connection->real_escape_string($contact->city);
  78.             $state = $db_connection->real_escape_string($contact->state);
  79.             $zipcode = $db_connection->real_escape_string($contact->zipcode);
  80.             $phone = $db_connection->real_escape_string($contact->phone);
  81.             $email = $db_connection->real_escape_string($contact->email);
  82.             $gender = $db_connection->real_escape_string($contact->gender);
  83.             $accept = $db_connection->real_escape_string($contact->accept);    
  84.    
  85.            
  86.             // Build the insert query
  87.             $query  = "INSERT INTO tbl_request_info (";
  88.             $query .= "  firstname, lastname, address1, address2, city, state, zipcode, phone, email, gender, accept";
  89.             $query .= ") VALUES (";
  90.             $query .= "  '{$firstname}', '{$lastname}', '{$address1}', '{$address2}', '{$city}', '{$state}', '{$zipcode}', '{$phone}', '{$email}', '{$gender}', '{$accept}' ";
  91.             $query .= ")";
  92.            
  93.             // Perform database insert query
  94.             $result = $db_connection->query($query);
  95.  
  96.             if ($result) {
  97.                 // Success
  98.                 redirect_to("thankyou.php");
  99.             } else {
  100.                 // Failure
  101.                 die("Database query failed. " . mysqli_error($connection));
  102.             }
  103.            
  104.         }
  105.        
  106.     } else {
  107.         // Form was not submitted by the submit button
  108.         $contact->resetform();
  109.     }
  110. ?>
  111. <!doctype html>
  112. <html lang="en-US">
  113. <head>
  114. <meta charset="UTF-8">
  115. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  116. <link rel="stylesheet" href="main.css" type="text/css" media="screen">
  117. <title>Placidus: Request Information</title>
  118. </head>
  119.  
  120. <body>
  121. <!-- Global Header and Logo -->
  122. <header id="page-header" role="banner">
  123.    <h1 id="main-logo-text"><a href="landing.html">Placidus</a></h1>
  124. </header>
  125.  
  126.  
  127. <!-- Site Navigation -->
  128. <nav id="main-navigation" role="navigation">
  129.    <h2>Site Navigation</h2>
  130.    <!-- used for html5 outline marking -->
  131.    <a href="#main-content" title="skip to main content" class="skip">Skip to main content</a> <!-- accessibility option to skip navigation -->
  132.    <ul>
  133.       <li><a href="landing.html" title="Home" >Home</a></li>
  134.       <li><a href="registration.php" title="Request Information" class="current">Request Information</a></li>
  135.       <li><a href="" title="Healthcare Professionals">Healthcare Professionals</a></li>
  136.    </ul>
  137. </nav>
  138.  
  139.  
  140.  
  141. <!-- Main Content -->
  142. <main role="main" id="main-content">
  143.    <article>
  144.     <section class="solo">
  145.       <h2>Request Information for Placidus</h2>      
  146.      
  147.       <form method="post" action="registration.php">
  148.             <p class="formnotes" style="text-align: right;">* indicates a required field</p>
  149.          <?php if ($errors || $missing) : ?>
  150.                 <p class="warning">Please fix the item(s) indicated</p>
  151.             <?php
  152.                 echo form_errors($errors);
  153.                 endif; ?>
  154.          
  155.          <!-- First Name -->
  156.          <label for="fd-fristname">First Name*:
  157.             <?php if ($missing && in_array("firstname", $missing)) : ?>
  158.                 <span class="warning">Please enter your first name.</span>
  159.             <?php endif; ?>
  160.             </label>
  161.          <input type="text" name="firstname" id="fd-firstname" maxlength="50" placeholder="First Name" class="formfullwidth" value="<?php echo htmlspecialchars($contact->firstname); ?>" required /><br />
  162.          
  163.          <!-- Last Name -->
  164.          <label for="fd-lastname">Last Name*:
  165.             <?php if ($missing && in_array("lastname", $missing)) : ?>
  166.                 <span class="warning">Please enter your last name.</span>
  167.             <?php endif; ?></label>
  168.         <input type="text" name="lastname" id="fd-lastname" maxlength="50" placeholder="Last Name" class="formfullwidth"  value="<?php echo htmlspecialchars($contact->lastname); ?>" required /><br />
  169.        
  170.          <!-- Address1 -->
  171.          <label for="fd-address1">Street Address*:
  172.             <?php if ($missing && in_array("address1", $missing)) : ?>
  173.                 <span class="warning">Please enter your street address.</span>
  174.             <?php endif; ?></label>
  175.         <input type="text" name="address1" id="fd-address1" maxlength="50" placeholder="1234 My Street" class="formfullwidth" value="<?php echo htmlspecialchars($contact->address1); ?>" required /><br />
  176.          
  177.          <!-- Address2 (optional) -->
  178.          <label for="fd-address2">Address Line 2:</label>
  179.         <input type="text" name="address2" id="fd-address2" maxlength="50" placeholder="(optional)" class="formfullwidth" value="<?php echo htmlspecialchars($contact->address2); ?>" /><br />
  180.          
  181.          <!-- City -->
  182.          <label for="fd-city">City*:
  183.             <?php if ($missing && in_array("city", $missing)) : ?>
  184.                 <span class="warning">Please enter the name of your city.</span>
  185.             <?php endif; ?></label>
  186.         <input type="text" name="city" id="fd-city" maxlength="50" placeholder="City" class="formfullwidth" value="<?php echo htmlspecialchars($contact->city); ?>" required /><br />
  187.          
  188.          <!-- State -->
  189.          <label for="fd-state">State*:
  190.             <?php if ($missing && in_array("state", $missing)) : ?>
  191.                 <span class="warning">Please enter the name of your state.</span>
  192.             <?php endif; ?></label>
  193.         <input type="text" name="state" id="fd-state" maxlength="50" placeholder="State" class="formfullwidth" value="<?php echo htmlspecialchars($contact->state); ?>" required /><br />
  194.          
  195.          <!-- Zip Code -->
  196.          <label for="fd-zipcode">Zip Code*:
  197.             <?php if ($missing && in_array("zipcode", $missing)) : ?>
  198.                 <span class="warning">Please enter the zipcode of your address.</span>
  199.             <?php endif; ?></label>
  200.         <input type="text" name="zipcode" id="fd-zipcode" maxlength="10" placeholder="12345" class="formfullwidth" value="<?php echo htmlspecialchars($contact->zipcode); ?>" required /><br />
  201.          
  202.          <!-- Phone -->
  203.          <label for="fd-phone">Phone*:
  204.             <?php if ($missing && in_array("phone", $missing)) : ?>
  205.                 <span class="warning">Please enter your prefered phone number.</span>
  206.             <?php endif; ?></label>
  207.         <input type="tel" name="phone" id="fd-phone" maxlength="30" placeholder="1234567890" class="formfullwidth" value="<?php echo htmlspecialchars($contact->phone); ?>" required /><br />
  208.          
  209.          <!-- Email -->
  210.          <label for="fd-email">E-mail*:
  211.             <?php if ($missing && in_array("email", $missing)) : ?>
  212.                 <span class="warning">Please enter your main e-mail address.</span>
  213.             <?php endif; ?></label>
  214.         <input type="email" name="email" id="fd-email" maxlength="50" placeholder="name@email.com" class="formfullwidth" value="<?php echo htmlspecialchars($contact->email); ?>" required /><br />
  215.        
  216.          <!-- Gender Radio Group -->
  217.          <fieldset>
  218.             <legend>Gender*:
  219.                 <?php if ($missing && in_array("gender", $missing)) : ?>
  220.                     <span class="warning">Please select a gender.</span>
  221.                 <?php endif; ?></legend>
  222.             <p>
  223.                 <!-- Female option -->
  224.                 <input type="radio" name="gender" id="fd-gender_f" value="female"
  225.                 <?php
  226.                             if ($_POST && $contact->gender == "female") {
  227.                                 echo "checked ";
  228.                             }
  229.                         ?>
  230.                required >
  231.                 <label for="fd-gender_f">Female</label><br/>
  232.                  
  233.                <!-- Male option -->
  234.                 <input type="radio" name="gender" id="fd-gender_m" value="male" <?php
  235.                             if ($_POST && $contact->gender == "male") {
  236.                                 echo "checked ";
  237.                             }
  238.                         ?>
  239.                required >
  240.                 <label for="fd-gender_m">Male</label><br/>
  241.             </p>
  242.          </fieldset>
  243.          
  244.          <!-- Marketing Agreement -->
  245.         <p class="formnotes">In order to recieve information for Placidus you must agree to the following terms: You are 18 years of age or older. By providing your name an other personal information above you agree to permit PharmaObscuro to use this informaiton to provide you with information and materials associated with Placidus.</p>
  246.          
  247.          <!-- Agreement Checkbox -->
  248.          <label for="fd-accept">I accept the above terms: <?php if ($missing && in_array("accept", $missing)) : ?>
  249.                     <span class="warning">Agreement to the terms is required.</span>
  250.                 <?php endif; ?></label>
  251.            
  252.          <input type="checkbox" name="accept" id="fd-accept" value="agreed" <?php
  253.                             if ($_POST && $contact->accept == "agreed") {
  254.                                 echo "checked ";
  255.                             }
  256.                         ?> required><br />
  257.                   <br />
  258.         <!-- Math Captcha -->
  259.          <fieldset>
  260.          <legend>Captcha:
  261.             <?php if ($missing && in_array("captcha_result", $missing)) : ?>
  262.                 <span class="warning">Please answer the captcha.</span>
  263.             <?php endif; ?></legend>
  264.          <?php  //Generate numbers for captcha.
  265.                 $min_number = 1;
  266.                 $max_number = 15;
  267.  
  268.                 $random_number1 = mt_rand($min_number, $max_number);
  269.                 $random_number2 = mt_rand($min_number, $max_number);
  270.             ?>
  271.             <p>What is <?php echo $random_number1 . ' + ' . $random_number2 . ' = '; ?><input name="captcha_result" type="text" /></p><br />
  272.             <input name="first_number" type="hidden" value="<?php echo $random_number1; ?>" />
  273.                 <input name="second_number" type="hidden" value="<?php echo $random_number2; ?>" />
  274.          </fieldset>
  275.          
  276.          <!-- Form Button -->
  277.          <input type="submit" name="submit" value="Request Information" class="formbutton" />
  278.     </form>
  279.         <!-- end of form -->
  280.      
  281.      
  282.       </section>
  283.    </article>
  284. </main>
  285. <!-- end of main content -->
  286.  
  287.  
  288.  
  289. <!-- Footer -->
  290. <footer id="page-footer">
  291.    <nav id="footer-navigation" role="contentinfo">
  292.       <h2>Footer Navigation</h2>
  293.       <ul>
  294.          <li><a href="" title="Site Map">Site Map</a></li>
  295.          <li><a href="" title="Privacy Policy" class="skip">Privacy Policy</a></li>
  296.          <li><a href="" title="Terms of Use" class="skip">Terms of Use</a></li>
  297.       </ul>
  298.    </nav>
  299.    <p class="footer-tail">Copyright &copy; 2016, PharmaObscuro LLC. All rights reserved.</p>
  300. </footer>
  301. </body>
  302. </html>
  303. <?php
  304.     // Close the database connection.
  305.     $db_connection->close();
  306. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement