Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_REQUEST['submit']))
- {
- $error = validate();
- if($error)
- {
- form_page($error);
- }
- else
- {
- output_page( );
- }
- }
- else
- {
- form_page('');
- }
- ?>
- <html>
- <?php
- function validate()
- {
- $reg_exp1 = "/^[a-zA-Z\-\']+$/";
- $reg_exp2 = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$";
- $id_number = trim($_REQUEST['idnumber']);
- $first_name = trim($_REQUEST['firstname']);
- $last_name = trim($_REQUEST['lastname']);
- $address = trim($_REQUEST['address']);
- $email = trim($_REQUEST['email']);
- $phone = trim($_REQUEST['phone']);
- $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
- $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
- $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
- $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
- $error ='';
- if (!is_numeric($id_number))
- {
- $error .= "<span class=\"error\"> Id is invalid. (format, Numbers only)</span><br>";
- }
- if(! preg_match($reg_exp1, $first_name))
- {
- $error .= "<span class=\"error\">First name is invalid (letters,hyphens, ',only)</span><br>";
- }
- if(! preg_match($reg_exp1, $last_name))
- {
- $error .= "<span class=\"error\">Last name is invalid (letters,hyphens, ',only)</span><br>";
- }
- if(! eregi($reg_exp2, $email))
- {
- $error .= "<span class=\"error\"> Email is invalid. (Format as xxxx@yyyy only)</span><br>";
- }
- if (! eregi("^[[:digit:]]{3}-[[:digit:]]{7}$",$phone))
- {
- $error .= "<span class=\"error\"> Phone number invalid. (Format as 999-9999999)</span><br>";
- }
- if( empty($often))
- {
- $error .="<span class=\"error\"> Select how often you take exercise</span><br>";
- }
- if(! is_array($why))
- {
- $error .= "<span class=\"error\"> You must select your reason for joining the gym.</span><br>";
- }
- ?>
- <?php
- return $error;
- }
- ?>
- <?php
- function output_page()
- {
- $id_number = trim($_REQUEST['idnumber']);
- $first_name = trim($_REQUEST['firstname']);
- $last_name = trim($_REQUEST['lastname']);
- $address = trim($_REQUEST['address']);
- $email = trim($_REQUEST['email']);
- $phone = trim($_REQUEST['phone']);
- $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
- $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
- $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
- $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
- ?>
- <html>
- <head><title> Form Results</title></head>
- <body>
- <h1>Form Results</h1>
- <br>
- <p> Please review your answers: </p>
- <br>
- <form action = mailto:youremail@email.com method = "post">
- <textarea rows="20" cols="60"wrap="physical" name ="comments">
- <?php
- echo " \n
- ID: $id_number \n
- First name: $first_name \n
- Last name: $last_name \n
- Address: $address \n
- Email: $email \n
- Phone: $phone \n
- Health: $health \n
- Frequency of visits to gym: $often \n
- You wish to join for the following reasons: $why \n
- Comments: $comments \n
- "
- ?>
- </textarea>
- <br>
- <br>
- <input type="submit"value ="Send Thru Email" name="submit">
- <br>
- <br>
- </form>
- <html>
- <a href="ianfloodB00035139assignment2.php"> Back to form </a>
- </html>
- <?php
- }
- ?>
- <?php
- function form_page($error)
- {
- $self = $_SERVER['PHP_SELF'];
- if($error)
- {
- echo "<FONT COLOR='red'><p>$error</></font>\n";
- $id_number = trim($_REQUEST['idnumber']) ? $_REQUEST['idnumber'] : '';
- $first_name = trim($_REQUEST['firstname']) ? $_REQUEST['firstname'] : '';
- $last_name = trim($_REQUEST['lastname']) ? $_REQUEST['lastname'] : '';
- $address = trim($_REQUEST['address']) ? $_REQUEST['address'] : '';
- $email = trim($_REQUEST['email']) ? $_REQUEST['email'] : '';
- $phone = trim($_REQUEST['phone']) ? $_REQUEST['phone'] : '';
- $why = ($_REQUEST['why']) ? $_REQUEST['why'] : '';
- $often = ($_REQUEST['often']) ? $_REQUEST['often'] : '';
- $health = ($_REQUEST['health']) ? $_REQUEST['health'] : '';
- $comments = ($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
- }
- ?>
- <html>
- <style>
- h2 {color: red;
- background: #CCCC66;}
- #sectionone {background: #FF33CC;}
- #sectiontwo {background: #FFCC66; }
- #sectionthree {background: #99FF66; }
- #textfield {margin-left: 20px;}
- </style>
- <body>
- <h1> Gym Questionnaire </h1>
- <p> Please fill out the form below </p>
- <div id= "sectionone">
- <h2>Personal information </h2>
- <form action="<?php echo $self?>" method="get">
- ID Number: <input type="text" size="12" maxlength="30" name="idnumber" value = "<?php echo $id_number?>"/> <br>
- Firstname: <input type="text" size="12" maxlength="30" name="firstname" value = "<?php echo $first_name?>"/> <br>
- Lastname: <input type="text" size="12" maxlength="30" name="lastname" value = "<?php echo $last_name?>"/> <br>
- Address: <input type="text" size="12" maxlength="30" name="address" value = "<?php echo $address?>"/> <br>
- Email: <input type="text" size="12" maxlength="30" name="email" value = "<?php echo $email?>"/> <br>
- Phone: <input type="text" size="12" maxlength="30" name="phone" value = "<?php echo $phone?>"/> <br>
- <br>
- </div>
- <div id= "sectiontwo">
- <h2>Health </h2>
- What is your general health like?:
- <select name="health"
- <option selected="selected">Above average. </option>
- <option>Average. </option>
- <option>Below Average. </option>
- </select>
- <br>
- <br>
- How often do you go to the gym each week?:
- <br>
- <input type="radio" name="often"value="less"<?php check2($often,"less") ?>> Less than once
- <br>
- <input type="radio" name="often"value="once"<?php check2($often,"once") ?>> once:
- <br>
- <input type="radio" name="often"value="two"<?php check2($often,"two") ?>> 2-3 times
- <br>
- <input type="radio" name="often"value="fourmore"<?php check2($often,"fourormore") ?>> 4- times
- <br>
- <br>
- Why do you go to the gym? (Tick all that apply)
- <br>
- Health improvement<Input type="checkbox" name="why[]"value ="Health improvement"<?php check($why,"Health improvement") ?>>
- <br>
- Rehabilitation<Input type="checkbox" name="why[]" value = "Rehabilitation"<?php check($why,"Rehabilitation") ?> />
- <br>
- Strength Training<Input type="checkbox" name="why[]" value = "Strength Training"<?php check($why,"Strength Training") ?> />
- <br>
- Weight loss<Input type="checkbox" name="why[]" value = "Weight loss"<?php check($why,"Weight loss") ?> />
- <br>
- Lifestyle<Input type="checkbox" name="why[]" value = "Lifestyle"<?php check($why,"Lifestyle") ?> />
- <br>
- <br>
- </div>
- <div id= "sectionthree">
- <h2>Comments </h2>
- <br>
- <div id= "textfield">
- <textarea rows="10" cols="20"wrap="physical" name ="comments">
- Enter any comments here!
- </textarea>
- </div>
- </div>
- <br>
- <br>
- <input type="submit"value ="Submit" name="submit">
- <input type="reset" value="Start over" name="Clear">
- </form>
- <?php
- }
- /*check that $checkboxes is an array and that val(s) exsist and place a tic*/
- function check($why,$val)
- {
- if (is_array($why) and in_array($val, $why))
- {
- echo 'checked = "checked"';
- }
- }
- function check2($often, $val2)
- {
- if ($often === $val2)
- {
- echo 'checked = "checked"';
- }
- }//end of fuction2
- function check3($comments, $val3)
- {
- if($comments == $val3)
- {
- echo 'checked = "checked"';
- }
- }
- ?>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement