Advertisement
commandrea

PHP Form, validate in same page

Feb 28th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. <?php
  2. if($_POST['formSubmit'] == "Submit")
  3. {
  4. $errorMessage = "";
  5.  
  6. if(empty($_POST['agreeName']))
  7. {
  8. $errorMessage .= "<li>You forgot to sign the release.</li>";
  9. }
  10.  
  11. if(empty($_POST['last_name']))
  12. {
  13. $errorMessage .= "<li>You forgot to enter your a last name!</li>";
  14. }
  15.  
  16. if(empty($_POST['first_name']))
  17. {
  18. $errorMessage .= "<li>You forgot to enter your first name!</li>";
  19. }
  20.  
  21. if(empty($_POST['age']))
  22. {
  23. $errorMessage .= "<li>You forgot to enter your age.</li>";
  24. }
  25.  
  26. if(empty($_POST['gender']))
  27. {
  28. $errorMessage .= "<li>You forgot to enter your gender.</li>";
  29. }
  30.  
  31. if(empty($_POST['phone']))
  32. {
  33. $errorMessage .= "<li>You forgot to enter your phone number.</li>";
  34. }
  35.  
  36. if(empty($_POST['email']))
  37. {
  38. $errorMessage .= "<li>You forgot to enter an email!</li>";
  39. }
  40.  
  41. if(empty($_POST['street_no']))
  42. {
  43. $errorMessage .= "<li>You need to enter the address where you currently reside.</li>";
  44. }
  45.  
  46. if(empty($_POST['city']))
  47. {
  48. $errorMessage .= "<li>You need to enter the city where you currently reside.</li>";
  49. }
  50.  
  51. if(empty($_POST['state']))
  52. {
  53. $errorMessage .= "<li>You need to enter the state where you currently reside.</li>";
  54. }
  55.  
  56. if(empty($_POST['zip']))
  57. {
  58. $errorMessage .= "<li>You need to enter your zipcode.</li>";
  59. }
  60.  
  61. if(empty($_POST['attended']))
  62. {
  63. $errorMessage .= "<li>Please answer the question: Have you attended...</li>";
  64. }
  65.  
  66. if(empty($_POST['volbefore']))
  67. {
  68. $errorMessage .= "<li>Please answer the question: Have you volunteered...</li>";
  69. }
  70.  
  71. if(empty($_POST['volbeforeyes']))
  72. {
  73. $errorMessage .= "<li>Please answer the question: If you have volunteered...</li>";
  74. }
  75.  
  76.  
  77. if(empty($_POST['cpr']))
  78. {
  79. $errorMessage .= "<li>Please answer the question: Are you CPR certified?</li>";
  80. }
  81.  
  82. if(empty($_POST['tabc']))
  83. {
  84. $errorMessage .= "<li>Please answer the question: Are you TABC certified?</li>";
  85. }
  86.  
  87. if(empty($_POST['skills']))
  88. {
  89. $errorMessage .= "<li>Please answer the question: What are your skills?</li>";
  90. }
  91.  
  92. if(empty($_POST['avail']))
  93. {
  94. $errorMessage .= "<li>Please answer the question: What is your availability?</li>";
  95. }
  96.  
  97.  
  98. if(empty($_POST['shirt']))
  99. {
  100. $errorMessage .= "<li>Please tell us your shirt size.</li>";
  101. }
  102.  
  103.  
  104.  
  105.  
  106. $agreeName = $_POST['agreeName'];
  107. $last_name = $_POST['last_name'];
  108. $first_name = $_POST['first_name'];
  109. $age = $_POST['age'];
  110. $gender = $_POST['gender'];
  111. $phone = $_POST['phone'];
  112. $email = $_POST['email'];
  113. $facebook = $_POST['facebook'];
  114. $street_no = $_POST['street_no'];
  115. $city = $_POST['city'];
  116. $state = $_POST['state'];
  117. $zip = $_POST['zip'];
  118. $attended = $_POST['attended'];
  119. $volbefore = $_POST['volbefore'];
  120. $volbeforeyes = $_POST['volbeforeyes'];
  121. $cpr = $_POST['cpr'];
  122. $tabc = $_POST['tabc'];
  123. $skills = $_POST['skills'];
  124. $avail = $_POST['avail'];
  125. $shirt = $_POST['shirt'];
  126.  
  127. if(empty($errorMessage))
  128. {
  129.  
  130.  
  131. $db = mysql_connect("host", "DBNAME", "pword");
  132. if(!$db) die("Error connecting to MySQL database.");
  133. mysql_select_db("DBNAME" ,$db);
  134.  
  135. function checkEmail($email) {
  136. $email = mysql_real_escape_string($email);
  137.  
  138. $sql = mysql_query("SELECT * FROM vols2012 WHERE email='$email'");
  139.  
  140. if (mysql_num_rows($sql) == 0) {
  141. return true;
  142. }
  143.  
  144. return false;
  145. }
  146.  
  147. if (checkEmail($_POST['email'])) {
  148. // Continue insert
  149. } else {die( "There is already a user with that email!" ) ;
  150. }//end if
  151.  
  152.  
  153.  
  154.  
  155. $sql = "INSERT INTO vols2012 (agreeName, last_name, first_name, age, gender, phone, email, facebook, street_no, city, state, zip, attended, volbefore, volbeforeyes, cpr, tabc, skills, avail, shirt)
  156.  
  157. VALUES (".
  158.  
  159. PrepSQL($agreeName) . ", " .
  160. PrepSQL($last_name) . ", " .
  161. PrepSQL($first_name) . ", " .
  162. PrepSQL($age) . ", " .
  163. PrepSQL($gender) . ", " .
  164. PrepSQL($phone) . ", " .
  165. PrepSQL($email) . ", " .
  166. PrepSQL($facebook) . ", " .
  167. PrepSQL($street_no) . ", " .
  168. PrepSQL($city) . ", " .
  169. PrepSQL($state) . ", " .
  170. PrepSQL($zip) . ", " .
  171. PrepSQL($attended) . ", " .
  172. PrepSQL($volbefore) . ", " .
  173. PrepSQL($volbeforeyes) . ", " .
  174. PrepSQL($cpr) . ", " .
  175. PrepSQL($tabc) . ", " .
  176. PrepSQL($skills) . ", " .
  177. PrepSQL($avail) . ", " .
  178. PrepSQL($shirt) . ")";
  179.  
  180. mysql_query($sql);
  181. header("Location: volthankyou.php");
  182. exit();
  183. }
  184. }
  185.  
  186.  
  187.  
  188. // function: PrepSQL()
  189. // use stripslashes and mysql_real_escape_string PHP functions
  190. // to sanitize a string for use in an SQL query
  191. //
  192. // also puts single quotes around the string
  193. //
  194. function PrepSQL($value)
  195. {
  196. // Stripslashes
  197. if(get_magic_quotes_gpc())
  198. {
  199. $value = stripslashes($value);
  200. }
  201.  
  202. // Quote
  203. $value = "'" . mysql_real_escape_string($value) . "'";
  204.  
  205. return($value);
  206. }
  207. ?>
  208.  
  209. <!DOCTYPE html>
  210. <html>
  211. <head>
  212. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  213.  
  214. </head>
  215.  
  216.  
  217.  
  218.  
  219. <body>
  220.  
  221. <!-- Form Left start -->
  222.  
  223.  
  224.  
  225. <?php
  226. if(!empty($errorMessage))
  227. {
  228. echo("<p>There was an error with your form:</p>\n");
  229. echo("<ul>" . $errorMessage . "</ul>\n");
  230. }
  231. ?>
  232.  
  233. <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
  234.  
  235.  
  236. <div id="volApp" class="form_left">
  237.  
  238.  
  239. <fieldset>
  240. <br /> <br />
  241.  
  242. <label>First name<br /><input type="text" name="first_name" placeholder="Contact First Name" value="<?=$first_name;?>" /></label>
  243.  
  244. <label>Last name<br /><input type="text" name="last_name" placeholder="Contact Last Name" value="<?=$last_name;?>" /></label>
  245.  
  246. Age <input style="height: 10px; margin-right: 12px; width: 18px" type="text" name="age" value="<?=$age;?>" /> Gender <input type="radio" name="gender" value="female">F <input type="radio" name="gender" value="male" checked>M
  247. <br />
  248. <label>Contact phone<br /><input type="text" name="phone" placeholder="xxx-xxx-xxxx" value="<?=$phone;?>" /></label>
  249.  
  250. <label>Email<br /><input type="email" name="email" placeholder="Email Address" value="<?=$email;?>" /></label>
  251.  
  252. <label>Street<br /><input type="text" name="street_no" placeholder="Mailing Address" value="<?=$street_no;?>" /></textarea></label>
  253.  
  254. <label>City<br /><input type="text" name="city" placeholder="City" value="<?=$city;?>" /></label>
  255.  
  256. <label>State<br /><input type="text" name="state" placeholder="State" value="<?=$state;?>" /></label>
  257.  
  258. <label>Zip<br /><input type="text" name="zip" placeholder="Zipcode" value="<?=$zip;?>" /></label>
  259.  
  260. <label>Have you volunteered for the fest before?
  261. <input type="radio" name="volbefore" value="yes">YES
  262. <input type="radio" name="volbefore" value="no" checked>NO<br/ >
  263. </label>
  264.  
  265. <label>If no, have you attended the fest as a guest?
  266. <input type="radio" name="attended" value="yes">YES
  267. <input type="radio" name="attended" value="no" checked>NO<br/ >
  268. </label>
  269.  
  270.  
  271. <label>If yes, what year(s), shifts/positions did you cover?<br/ >
  272. <textarea placeholder="Ex. 2010, 2011, Sat. pm/Sun. am, wristbander, first aid, booth" name="volbeforeyes" id="bigarea" value="<?=$volbeforeyes;?>" /></textarea></label>
  273.  
  274. <label>Are you TABC certified?
  275. <input type="radio" name="tabc" value="yes">YES
  276. <input type="radio" name="tabc" value="no" checked>NO<br/ >
  277. </label>
  278.  
  279. <label>Are you CPR certified?
  280. <input type="radio" name="cpr" value="yes">YES
  281. <input type="radio" name="cpr" value="no" checked>NO<br/ >
  282. </label>
  283.  
  284. <label>List skills you have that can be put to good use at the fest.<br/ >
  285. <textarea placeholder="Ex. CPR, strong, math, loud, can't stop/won't stop (No photographers please)" name="skills" id="bigarea" value="<?= $skills;?>" /></textarea></label>
  286.  
  287. <label>A.M. shifts run from around 9am to 4pm and P.M. shifts from 3:30pm to around 11pm. While it's a ways off, do you currently know of any restrictions to your schedule that would prevent you from being available for any of these shifts?<br/ >
  288. <textarea placeholder="Ex.: NOPE, All yours!, Yes- Graduation Sat A.M." name="avail" id="bigarea" value="<?= $avail;?>" /></textarea></label>
  289.  
  290. <label>What's your name on Facebook?<br />
  291. <input type="text" name="facebook" value="<?= $facebook;?>" /> </label>
  292.  
  293. <label>T-SHIRT SIZE
  294. <input type="radio" name="shirt" value="small" checked>S
  295. <input type="radio" name="shirt" value="medium"> M
  296. <input type="radio" name="shirt" value="large" >L
  297. <input type="radio" name="shirt" value="XL"> XL
  298. <input type="radio" name="shirt" value="XXL"> XXL
  299. </label>
  300.  
  301.  
  302. <br /><br />
  303.  
  304. </fieldset>
  305. </div>
  306.  
  307.  
  308. <!-- Form Left end -->
  309. <!-- Form Right start -->
  310.  
  311. <div class="form_right">
  312.  
  313.  
  314. <fieldset>
  315.  
  316. <div id="release">
  317.  
  318. <h2 class="bigboldfont">RELEASE</h2>
  319.  
  320. <label>First and Last name <input style="width:230px; margin-top:10px" type="text" name="agreeName" value="<?=$agreeName;?>" /></label>
  321.  
  322. </div>
  323.  
  324.  
  325.  
  326. <div style="height:60px">
  327.  
  328. <div id="verQ"><label>Spam verification: 3+1= <input type="text" name="hii" placeholder="?"></label></div>
  329.  
  330.  
  331. <input class="float-right btnSubmit" type="submit" value="Submit" name="formSubmit" id="submit">
  332.  
  333.  
  334.  
  335. </div>
  336.  
  337. </fieldset>
  338. </div>
  339.  
  340.  
  341.  
  342. </form>
  343.  
  344. </body>
  345. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement