Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.25 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.     $self = $_REQUEST['PHP_SELF'];
  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="<?php $self ?>"> 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. if($error)
  159. {
  160.     echo "<FONT COLOR='red'><p>$error</></font>\n";
  161.  
  162.     $id_number = trim($_REQUEST['idnumber']) ? $_REQUEST['idnumber'] : '';
  163.     $first_name = trim($_REQUEST['firstname']) ? $_REQUEST['firstname'] : '';
  164.     $last_name = trim($_REQUEST['lastname']) ? $_REQUEST['lastname'] : '';
  165.     $address = trim($_REQUEST['address']) ? $_REQUEST['address'] : '';
  166.     $email = trim($_REQUEST['email']) ? $_REQUEST['email'] : '';
  167.     $phone = trim($_REQUEST['phone']) ? $_REQUEST['phone'] : '';
  168.     $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
  169.     $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
  170.     $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
  171.     $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
  172. }
  173. ?>
  174. <html>
  175.  
  176. <style>
  177.  
  178. h2 {color: red;
  179.     background: #CCCC66;}
  180.  
  181. #sectionone {background: #FF33CC;}
  182.  
  183. #sectiontwo {background: #FFCC66; }
  184.  
  185. #sectionthree {background: #99FF66; }
  186.  
  187. #textfield {margin-left: 20px;}
  188. </style>
  189.  
  190. <body>
  191.  
  192. <h1> Gym Questionnaire </h1>
  193.  
  194. <p> Please fill out the form below </p>
  195.  
  196.     <div id= "sectionone">
  197.     <h2>Personal information </h2>
  198.  
  199.  
  200.     <form action="<?php echo $self?>" method="get">
  201.     ID Number: <input type="text" size="12" maxlength="30" name="idnumber" value = "<?php echo $id_number?>"/> <br>
  202.  
  203.     Firstname: <input type="text" size="12" maxlength="30" name="firstname" value = "<?php echo $first_name?>"/> <br>
  204.  
  205.     Lastname: <input type="text" size="12" maxlength="30" name="lastname" value = "<?php echo $last_name?>"/> <br>
  206.  
  207.     Address: <input type="text" size="12" maxlength="30" name="address" value = "<?php echo $address?>"/> <br>
  208.  
  209.     Email: <input type="text" size="12" maxlength="30" name="email" value = "<?php echo $email?>"/> <br>
  210.  
  211.     Phone: <input type="text" size="12" maxlength="30" name="phone" value = "<?php echo $phone?>"/> <br>
  212.     <br>
  213.     </div>
  214.  
  215.  
  216.     <div id= "sectiontwo">
  217.     <h2>Health </h2>
  218.  
  219.     What is your general health like?:
  220.                                        <select name="health"
  221.                                        <option selected="selected">Above average. </option>
  222.                                        <option>Average.  </option>
  223.                                        <option>Below Average. </option>
  224.                                        </select>
  225.     <br>
  226.     <br>
  227.  
  228.     How often do you go to the gym each week?:
  229.      <br>
  230.      <input type="radio" name="often"value="less"<?php check2($often,"less") ?>> Less than once
  231.      <br>
  232.     <input type="radio" name="often"value="once"<?php check2($often,"once") ?>> once:
  233.      <br>
  234.     <input type="radio" name="often"value="two"<?php check2($often,"two") ?>> 2-3 times
  235.     <br>
  236.     <input type="radio" name="often"value="fourmore"<?php check2($often,"fourormore") ?>> 4- times
  237.  
  238.     <br>
  239.     <br>
  240.  
  241.  
  242.     Why do you go to the gym? (Tick all that apply)
  243.     <br>
  244.     Health improvement<Input type="checkbox" name="why[]"value ="Health improvement"<?php check($why,"Health improvement") ?>>
  245.     <br>
  246.     Rehabilitation<Input type="checkbox" name="why[]" value = "Rehabilitation"<?php check($why,"Rehabilitation") ?> />
  247.     <br>
  248.     Strength Training<Input type="checkbox" name="why[]" value = "Strength Training"<?php check($why,"Strength Training") ?> />
  249.     <br>
  250.     Weight loss<Input type="checkbox" name="why[]" value = "Weight loss"<?php check($why,"Weight loss")  ?> />
  251.     <br>
  252.     Lifestyle<Input type="checkbox" name="why[]" value = "Lifestyle"<?php check($why,"Lifestyle") ?> />
  253.     <br>
  254.     <br>
  255.  
  256.     </div>
  257.  
  258. <div id= "sectionthree">
  259.     <h2>Comments </h2>
  260.     <br>
  261.     <div id= "textfield">
  262.     <textarea rows="10" cols="20"wrap="physical" name ="comments">
  263.     Enter any comments here!
  264.     </textarea>
  265.     </div>
  266. </div>
  267.     <br>
  268.     <br>
  269.     <input type="submit"value ="Submit" name="submit">
  270.     <input type="reset" value="Start over" name="Clear">
  271.  
  272.  
  273.     </form>
  274. <?php
  275.  
  276. }
  277. /*check that $checkboxes is an array and that val(s) exsist and place a tic*/
  278. function check($why,$val)
  279. {
  280.     if (is_array($why) and in_array($val, $why))
  281.     {
  282.     echo 'checked = "checked"';
  283.     }
  284. }
  285.  
  286. function check2($often, $val2)
  287. {
  288.    if ($often === $val2)
  289.    {
  290.       echo 'checked = "checked"';
  291.    }
  292. }//end of fuction2
  293.  
  294. function check3($comments, $val3)
  295. {
  296.     if($comments == $val3)
  297.     {
  298.         echo 'checked = "checked"';
  299.     }
  300. }
  301.  
  302.  
  303.  
  304.  
  305. ?>
  306.  
  307.  
  308.  
  309. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement