Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.27 KB | None | 0 0
  1. <?php
  2.  
  3. if (isset($_REQUEST['submit']))
  4. {
  5.     $error = validate();
  6.     if($error)
  7.     {
  8.         form_page($error);
  9.     }
  10.     else
  11.     {
  12.         output_page( );
  13.     }
  14.  
  15. }
  16. else
  17. {
  18.     form_page('');
  19. }
  20.  
  21. ?>
  22.  
  23. <html>
  24. <?php
  25.  
  26. function validate()
  27. {
  28.     $reg_exp1 = "/^[a-zA-Z\-\']+$/";
  29.  
  30.     $reg_exp2 = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$";
  31.  
  32.     $id_number = trim($_REQUEST['idnumber']);
  33.     $first_name = trim($_REQUEST['firstname']);
  34.     $last_name = trim($_REQUEST['lastname']);
  35.     $address = trim($_REQUEST['address']);
  36.     $email = trim($_REQUEST['email']);
  37.     $phone = trim($_REQUEST['phone']);
  38.     $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
  39.     $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
  40.     $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
  41.     $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
  42.  
  43.     $error ='';
  44.  
  45.     if (!is_numeric($id_number))
  46.     {
  47.         $error .= "<span class=\"error\"> Id is invalid. (format, Numbers only)</span><br>";
  48.  
  49.     }
  50.  
  51.     if(! preg_match($reg_exp1, $first_name))
  52.     {
  53.         $error .= "<span class=\"error\">First name is invalid (letters,hyphens, ',only)</span><br>";
  54.     }
  55.  
  56.     if(! preg_match($reg_exp1, $last_name))
  57.     {
  58.         $error .= "<span class=\"error\">Last name is invalid (letters,hyphens, ',only)</span><br>";
  59.     }
  60.  
  61.     if(! eregi($reg_exp2, $email))
  62.     {
  63.         $error .= "<span class=\"error\"> Email is invalid. (Format as xxxx@yyyy only)</span><br>";
  64.     }
  65.  
  66.     if (! eregi("^[[:digit:]]{3}-[[:digit:]]{7}$",$phone))
  67.     {
  68.         $error .= "<span class=\"error\"> Phone number invalid. (Format as 999-9999999)</span><br>";
  69.     }
  70.  
  71.     if( empty($often))
  72.     {
  73.         $error .="<span class=\"error\"> Select how often you take exercise</span><br>";
  74.     }
  75.  
  76.     if(! is_array($why))
  77.     {
  78.         $error .= "<span class=\"error\"> You must select your reason for joining the gym.</span><br>";
  79.      }
  80.  
  81.     ?>
  82.  
  83. <?php
  84.  
  85.  
  86.  
  87.     return $error;
  88. }
  89. ?>
  90.  
  91. <?php
  92.  
  93. function output_page()
  94. {
  95.  
  96.     $id_number = trim($_REQUEST['idnumber']);
  97.     $first_name = trim($_REQUEST['firstname']);
  98.     $last_name = trim($_REQUEST['lastname']);
  99.     $address = trim($_REQUEST['address']);
  100.     $email = trim($_REQUEST['email']);
  101.     $phone = trim($_REQUEST['phone']);
  102.     $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
  103.     $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
  104.     $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
  105.     $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
  106.     ?>
  107.     <html>
  108.     <head><title> Form Results</title></head>
  109.     <body>
  110.     <h1>Form Results</h1>
  111.     <br>
  112.     <p> Please review your answers: </p>
  113.     <br>
  114.  
  115.     <form action = mailto:youremail@email.com method = "post">
  116.  
  117.     <textarea rows="20" cols="60"wrap="physical" name ="comments">
  118.  
  119.     <?php
  120.     echo " \n
  121. ID: $id_number \n
  122. First name: $first_name \n
  123. Last name: $last_name \n
  124. Address: $address     \n
  125. Email: $email          \n
  126. Phone: $phone        \n
  127. Health: $health           \n
  128. Frequency of visits to gym: $often     \n
  129. You wish to join for the following reasons: $why \n
  130. Comments: $comments \n
  131. "
  132.  
  133.            ?>
  134.  
  135.     </textarea>
  136.     <br>
  137.     <br>
  138.     <input type="submit"value ="Send Thru Email" name="submit">
  139.     <br>
  140.     <br>
  141.     </form>
  142.     <html>
  143.     <a href="ianfloodB00035139assignment2.php"> Back to form </a>
  144.     </html>
  145.  
  146.  
  147.  
  148.  
  149. <?php
  150. }
  151. ?>
  152.  
  153.  
  154. <?php
  155.  
  156. function form_page($error)
  157. {
  158.  
  159. $self = $_SERVER['PHP_SELF'];
  160.  
  161. if($error)
  162. {
  163.     echo "<FONT COLOR='red'><p>$error</></font>\n";
  164.  
  165.  
  166.     $id_number = trim($_REQUEST['idnumber']) ? $_REQUEST['idnumber'] : '';
  167.     $first_name = trim($_REQUEST['firstname']) ? $_REQUEST['firstname'] : '';
  168.     $last_name = trim($_REQUEST['lastname']) ? $_REQUEST['lastname'] : '';
  169.     $address = trim($_REQUEST['address']) ? $_REQUEST['address'] : '';
  170.     $email = trim($_REQUEST['email']) ? $_REQUEST['email'] : '';
  171.     $phone = trim($_REQUEST['phone']) ? $_REQUEST['phone'] : '';
  172.     $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
  173.     $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
  174.     $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
  175.     $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
  176. }
  177. ?>
  178. <html>
  179.  
  180. <style>
  181.  
  182. h2 {color: red;
  183.     background: #CCCC66;}
  184.  
  185. #sectionone {background: #FF33CC;}
  186.  
  187. #sectiontwo {background: #FFCC66; }
  188.  
  189. #sectionthree {background: #99FF66; }
  190.  
  191. #textfield {margin-left: 20px;}
  192. </style>
  193.  
  194. <body>
  195.  
  196. <h1> Gym Questionnaire </h1>
  197.  
  198. <p> Please fill out the form below </p>
  199.  
  200.     <div id= "sectionone">
  201.     <h2>Personal information </h2>
  202.  
  203.  
  204.     <form action="<?php echo $self?>" method="get">
  205.     ID Number: <input type="text" size="12" maxlength="30" name="idnumber" value = "<?php echo $id_number?>"/> <br>
  206.  
  207.     Firstname: <input type="text" size="12" maxlength="30" name="firstname" value = "<?php echo $first_name?>"/> <br>
  208.  
  209.     Lastname: <input type="text" size="12" maxlength="30" name="lastname" value = "<?php echo $last_name?>"/> <br>
  210.  
  211.     Address: <input type="text" size="12" maxlength="30" name="address" value = "<?php echo $address?>"/> <br>
  212.  
  213.     Email: <input type="text" size="12" maxlength="30" name="email" value = "<?php echo $email?>"/> <br>
  214.  
  215.     Phone: <input type="text" size="12" maxlength="30" name="phone" value = "<?php echo $phone?>"/> <br>
  216.     <br>
  217.     </div>
  218.  
  219.  
  220.     <div id= "sectiontwo">
  221.     <h2>Health </h2>
  222.  
  223.     What is your general health like?:
  224.                                        <select name="health"
  225.                                        <option selected="selected">Above average. </option>
  226.                                        <option>Average.  </option>
  227.                                        <option>Below Average. </option>
  228.                                        </select>
  229.     <br>
  230.     <br>
  231.  
  232.     How often do you go to the gym each week?:
  233.      <br>
  234.      <input type="radio" name="often"value="less"<?php check2($often,"less") ?>> Less than once
  235.      <br>
  236.     <input type="radio" name="often"value="once"<?php check2($often,"once") ?>> once:
  237.      <br>
  238.     <input type="radio" name="often"value="two"<?php check2($often,"two") ?>> 2-3 times
  239.     <br>
  240.     <input type="radio" name="often"value="fourmore"<?php check2($often,"fourormore") ?>> 4- times
  241.  
  242.     <br>
  243.     <br>
  244.  
  245.  
  246.     Why do you go to the gym? (Tick all that apply)
  247.     <br>
  248.     Health improvement<Input type="checkbox" name="why[]"value ="Health improvement"<?php check($why,"Health improvement") ?>>
  249.     <br>
  250.     Rehabilitation<Input type="checkbox" name="why[]" value = "Rehabilitation"<?php check($why,"Rehabilitation") ?> />
  251.     <br>
  252.     Strength Training<Input type="checkbox" name="why[]" value = "Strength Training"<?php check($why,"Strength Training") ?> />
  253.     <br>
  254.     Weight loss<Input type="checkbox" name="why[]" value = "Weight loss"<?php check($why,"Weight loss")  ?> />
  255.     <br>
  256.     Lifestyle<Input type="checkbox" name="why[]" value = "Lifestyle"<?php check($why,"Lifestyle") ?> />
  257.     <br>
  258.     <br>
  259.  
  260.     </div>
  261.  
  262. <div id= "sectionthree">
  263.     <h2>Comments </h2>
  264.     <br>
  265.     <div id= "textfield">
  266.     <textarea rows="10" cols="20"wrap="physical" name ="comments">
  267.     Enter any comments here!
  268.     </textarea>
  269.     </div>
  270. </div>
  271.     <br>
  272.     <br>
  273.     <input type="submit"value ="Submit" name="submit">
  274.     <input type="reset" value="Start over" name="Clear">
  275.  
  276.  
  277.     </form>
  278. <?php
  279.  
  280. }
  281. /*check that $checkboxes is an array and that val(s) exsist and place a tic*/
  282. function check($why,$val)
  283. {
  284.     if (is_array($why) and in_array($val, $why))
  285.     {
  286.     echo 'checked = "checked"';
  287.     }
  288. }
  289.  
  290. function check2($often, $val2)
  291. {
  292.    if ($often === $val2)
  293.    {
  294.       echo 'checked = "checked"';
  295.    }
  296. }//end of fuction2
  297.  
  298. function check3($comments, $val3)
  299. {
  300.     if($comments == $val3)
  301.     {
  302.         echo 'checked = "checked"';
  303.     }
  304. }
  305.  
  306.  
  307.  
  308.  
  309. ?>
  310.  
  311.  
  312.  
  313. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement