PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/orderpage.php

https://bitbucket.org/ahunting/april-10-projects-5-8
PHP | 350 lines | 303 code | 19 blank | 28 comment | 24 complexity | 23845c4340dc5350cc11e1bfd9884be0 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
  3. dir="ltr">
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <head>
  6. <title>Sandwich Shop</title>
  7. </head>
  8. <link rel="stylesheet" type="text/css" href="/style.css" />
  9. <body>
  10. <div style="font-weight: bold; margin-bottom: 20px">Welcome To Whole Foods Sandwich Shop
  11. </div>
  12. <div style="clear:both">
  13. &nbsp;
  14. </div>
  15. <?php
  16. /*
  17. create table orders (
  18. id int not null auto_increment,
  19. total decimal(10,2),
  20. address varchar(100),
  21. pay_type char(5),
  22. card_num int(20),
  23. month int(2),
  24. year int(4),
  25. cvv int(3),
  26. newsletter boolean,
  27. primary key(id)
  28. );
  29. */
  30. function fill_val($name, $id=0) {
  31. if ($_SERVER['REQUEST_METHOD']=='POST') {
  32. if (empty($_POST[$name])) {
  33. $val="";
  34. }elseif ($id<0) {
  35. if (isset($_POST[$name])) {
  36. $val='1';
  37. }else{
  38. $val='0';
  39. }
  40. }elseif ($id>0) {
  41. $var=$_POST[$name];
  42. $val=$var[$id];
  43. }else{
  44. $val=$_POST[$name];
  45. }
  46. }else{
  47. $val="";
  48. }
  49. return htmlspecialchars($val);
  50. }
  51. if ($_SERVER['REQUEST_METHOD']=='POST') {
  52. // connect to database
  53. $con = mysql_connect('2011.ispace.ci.fsu.edu', 'amh11u', 'cvrd2n8w');
  54. mysql_select_db('amh11u_sandwiches', $con);
  55. // retrieve the form
  56. $sandwiches = $_POST['sandwiches'];
  57. $quantities = $_POST['quantities'];
  58. $prices = $_POST['prices'];
  59. $address = mysql_real_escape_string($_POST['address'], $con);
  60. if (isset($_POST['pay_type'])) $pay_type = $_POST['pay_type'];
  61. $card_num = mysql_real_escape_string($_POST['card_num'], $con);
  62. $month = mysql_real_escape_string($_POST['month'], $con);
  63. $year = mysql_real_escape_string($_POST['year'], $con);
  64. $cvv = mysql_real_escape_string($_POST['cvv'], $con);
  65. $newsletter = 0;
  66. if (isset($_POST['newsletter'])) $newsletter=1;
  67. // check the input is valid
  68. $error=0;
  69. // check quantity
  70. $total=0;
  71. foreach ($quantities as $quantity) {
  72. if (!empty($quantity)) $total += $quantity;
  73. }
  74. if ($total==0) {
  75. print "<p style=\"color:red;\">You must input the quantity of the sandwiches</p>";
  76. $error++;
  77. }
  78. // check address
  79. if (empty($address)) {
  80. print "<p style=\"color:red;\">You must input the address</p>";
  81. $error++;
  82. }
  83. // check pay type
  84. if (empty($pay_type)) {
  85. print "<p style=\"color:red;\">You must input the pay type</p>";
  86. $error++;
  87. }else{
  88. // card number, expire date and cvv must be inputed when pay by credit card
  89. if ($pay_type=='credit') {
  90. if (empty($card_num)) {
  91. print "<p style=\"color:red;\">You must input card number when pay by credit card</p>";
  92. $error++;
  93. }
  94. if (empty($month)) {
  95. print "<p style=\"color:red;\">You must input expire month when pay by credit card</p>";
  96. $error++;
  97. }
  98. if (empty($year)) {
  99. print "<p style=\"color:red;\">You must input expire year when pay by credit card</p>";
  100. $error++;
  101. }
  102. if (empty($cvv)) {
  103. print "<p style=\"color:red;\">You must input cvv when pay by credit card</p>";
  104. $error++;
  105. }
  106. }
  107. }
  108. if ($error==0) {
  109. // calculate the total pay
  110. $total=0.0;
  111. foreach ($quantities as $id => $quantity) {
  112. if (!empty($quantity)) $total = $total + $prices[$id]*$quantity;
  113. }
  114. // add 7.5% tax
  115. $total = $total + $total * .075;
  116. // add 1.5% processing fee if pay by credit card
  117. if ($pay_type=='credit') {
  118. $total *= 1.015;
  119. }
  120. // construct the SQL statement
  121. if ($pay_type=='credit') {
  122. $sql ="INSERT INTO orders(total,address,pay_type,card_num,month,year,cvv,newsletter) ";
  123. $sql.="values($total,'$address','$pay_type',$card_num,$month,$year,$cvv,$newsletter)";
  124. }elseif ($pay_type=='cash') {
  125. $sql ="INSERT INTO orders(total,address,pay_type,newsletter) ";
  126. $sql.="values($total,'$address','$pay_type',$newsletter)";
  127. }
  128. // execute SQL statement
  129. mysql_query($sql, $con);
  130. // Is the SQL statment successfully executed?
  131. if (mysql_affected_rows($con)==1) {
  132. print "<p style=\"color:blue;\">Your order:</p>";
  133. print "<table>";
  134. print "<tr><td>sandwich</td><td>&nbsp;</td><td>price</td><td>&nbsp;</td><td>quantity</td></tr>";
  135. foreach ($quantities as $id => $quantity) {
  136. if (!empty($quantity)) {
  137. $sandwich=$sandwiches[$id];
  138. $price =$prices[$id];
  139. print "<tr><td>$sandwich</td><td>&nbsp;</td><td>$price</td><td>&nbsp;</td><td>$quantity</td></tr>";
  140. }
  141. }
  142. print "</table>";
  143. print "<p style=\"color:blue;\">Total :$".number_format($total,2)."</p>";
  144. }else{
  145. print '<p class="error">Could not submit your order:<br />'
  146. .mysql_error($con).'</p>';
  147. print $sql;
  148. }
  149. }
  150. // close the connection to database
  151. mysql_close($con);
  152. print "<hr>";
  153. }
  154. ?>
  155. <form action="orderpage.php" method="post">
  156. <table>
  157. <tr>
  158. <td>Name:</td>
  159. <td>The Sissy<input type="hidden" name="sandwiches[1]" value="The Sissy" /></td>
  160. <td align="right"><input name="quantities[1]" size="4" value="<?php print fill_val('quantities',1); ?>"/></td>
  161. </tr>
  162. <tr>
  163. <td>Price:</td>
  164. <td>$4.99</td>
  165. <td><input type="hidden" name="prices[1]" value="4.99" /></td>
  166. </tr>
  167. <tr>
  168. <td>Description:</td>
  169. <td colspan="2">Just veggies; no meat on a whole wheat bun</td>
  170. </tr>
  171. <tr>
  172. <td>Calories:</td>
  173. <td colspan="2">450</td>
  174. </tr>
  175. <tr>
  176. <td colspan="3"><hr></td>
  177. </tr>
  178. <tr>
  179. <td>Name:</td>
  180. <td>The Corleone<input type="hidden" name="sandwiches[2]" value="The Corleone" /></td>
  181. <td align="right"><input name="quantities[2]" size="4" value="<?php print fill_val('quantities',2); ?>"/></td>
  182. </tr>
  183. <tr>
  184. <td>Price:</td>
  185. <td>$5.50</td>
  186. <td><input type="hidden" name="prices[2]" value="5.50" /></td>
  187. </tr>
  188. <tr>
  189. <td>Description:</td>
  190. <td colspan="2">Ham and salami with lettuce tomato and onions</td>
  191. </tr>
  192. <tr>
  193. <td>Calories:</td>
  194. <td colspan="2">980</td>
  195. </tr>
  196. <tr>
  197. <td colspan="3"><hr></td>
  198. </tr>
  199. <tr>
  200. <td>Name:</td>
  201. <td>The Mediterranean<input type="hidden" name="sandwiches[3]" value="The Mediterranean" /></td>
  202. <td align="right"><input name="quantities[3]" size="4" value="<?php print fill_val('quantities',3); ?>" ></td>
  203. </tr>
  204. <tr>
  205. <td>Price:</td>
  206. <td>$6.99</td>
  207. <td><input type="hidden" name="prices[3]" value="6.99" /></td>
  208. </tr>
  209. <tr>
  210. <td>Description:</td>
  211. <td colspan="2">Hummus, turkey, tomatoes, and olives on whole wehat</td>
  212. </tr>
  213. <tr>
  214. <td>Calories:</td>
  215. <td colspan="2">500</td>
  216. </tr>
  217. <tr>
  218. <td colspan="3"><hr></td>
  219. </tr>
  220. <tr>
  221. <td>Name:</td>
  222. <td>The Greasy Pizza<input type="hidden" name="sandwiches[4]" value="The Greasy Pizza" /></td>
  223. <td align="right"><input name="quantities[4]" size="4" value="<?php print fill_val('quantities',4); ?>"/></td>
  224. </tr>
  225. <tr>
  226. <td>Price:</td>
  227. <td>$8.00</td>
  228. <td><input type="hidden" name="prices[4]" value="8.00" /></td>
  229. </tr>
  230. <tr>
  231. <td>Description:</td>
  232. <td colspan="2">Meatballs, sauce, pepperoni, and cheese on a white bun</td>
  233. </tr>
  234. <tr>
  235. <td>Calories:</td>
  236. <td colspan="2">1500</td>
  237. </tr>
  238. <tr>
  239. <td colspan="3"><hr></td>
  240. </tr>
  241. <tr>
  242. <td>Name:</td>
  243. <td>The Plain and Simple<input type="hidden" name="sandwiches[5]" value="The Plain and Simple" /></td>
  244. <td align="right"><input name="quantities[5]" size="4" value="<?php print fill_val('quantities',5); ?>"/></td>
  245. </tr>
  246. <tr>
  247. <td>Price:</td>
  248. <td>$5.50</td>
  249. <td><input type="hidden" name="prices[5]" value="5.50" /></td>
  250. </tr>
  251. <tr>
  252. <td>Description:</td>
  253. <td colspan="2">Turkey, lettuce, tomato, and mayo on a wheat bun</td>
  254. </tr>
  255. <tr>
  256. <td>Calories:</td>
  257. <td colspan="2">650</td>
  258. </tr>
  259. <tr>
  260. <td colspan="3"><hr></td>
  261. </tr>
  262. <tr>
  263. <td>Name:</td>
  264. <td>The Porker<input type="hidden" name="sandwiches[6]" value="The Porker" /></td>
  265. <td align="right"><input name="quantities[6]" size="4" value="<?php print fill_val('quantities',6); ?>"/></td>
  266. </tr>
  267. <tr>
  268. <td>Price:</td>
  269. <td>$7.99</td>
  270. <td><input type="hidden" name="prices[6]" value="7.99" /></td>
  271. </tr>
  272. <tr>
  273. <td>Description:</td>
  274. <td colspan="2">Pulled pork, bbq sauce, and cream cheese on a toasted wheat bun</td>
  275. </tr>
  276. <tr>
  277. <td>Calories:</td>
  278. <td colspan="2">1350</td>
  279. </tr>
  280. <tr>
  281. <td colspan="3"><hr></td>
  282. </tr>
  283. <tr>
  284. <td>Name:</td>
  285. <td>The Sub of Insanit<input type="hidden" name="sandwiches[7]" value="The Sub of Insanit" /></td>
  286. <td align="right"><input name="quantities[7]" size="4" value="<?php print fill_val('quantities',7); ?>"/></td>
  287. </tr>
  288. <tr>
  289. <td>Price:</td>
  290. <td>$9.99</td>
  291. <td><input type="hidden" name="prices[7]" value="9.99" /></td>
  292. </tr>
  293. <tr>
  294. <td>Description:</td>
  295. <td colspan="2">The works; all of our toppings on one sandwich</td>
  296. </tr>
  297. <tr>
  298. <td>Calories:</td>
  299. <td colspan="2">2200</td>
  300. </tr>
  301. <tr>
  302. <td colspan="3"><hr></td>
  303. </tr>
  304. <tr><td>Address</td><td>&nbsp;&nbsp; <textarea name="address" rows="4" cols="50"><?php print fill_val('address'); ?></textarea></td><td></td></tr>
  305. <tr><td>Pay type</td><td>&nbsp;&nbsp; <?php
  306. if (fill_val('pay_type')=='cash') {
  307. print "<input type=\"radio\" name=\"pay_type\" value=\"cash\" checked/>cash";
  308. }else{
  309. print "<input type=\"radio\" name=\"pay_type\" value=\"cash\" />cash";
  310. }
  311. if (fill_val('pay_type')=='credit') {
  312. print "<input type=\"radio\" name=\"pay_type\" value=\"credit\" checked/>credit";
  313. }else{
  314. print "<input type=\"radio\" name=\"pay_type\" value=\"credit\" />credit";
  315. }
  316. ?></td><td></td></tr>
  317. <tr><td>Card number</td><td>&nbsp;&nbsp; <input type="text" name="card_num" size="20" value="<?php print fill_val('card_num'); ?>" /></td><td></td></tr>
  318. <tr><td>Expire mm/yyyy</td><td>&nbsp;&nbsp; <input type="text" name="month" size="2" value="<?php print fill_val('month'); ?>" />/<input type="text" name="year" size="4" value="<?php print fill_val('year'); ?>" /></td><td></td></tr>
  319. <tr><td>CVV</td><td>&nbsp;&nbsp; <input type="text" name="cvv" size="3" value="<?php print fill_val('cvv'); ?>" /></td><td></td></tr>
  320. <tr><td>Subscribe newsletter</td><td>&nbsp;&nbsp; <?php
  321. if (fill_val('newsletter',-1)=='1') {
  322. print "<input type=\"checkbox\" name=\"newsletter\" checked/>";
  323. }else{
  324. print "<input type=\"checkbox\" name=\"newsletter\" />";
  325. }
  326. ?></td><td></td></tr>
  327. <tr><td></td><td>&nbsp;&nbsp; <input type="submit" name="submit" value="order sandwich"/></td><td></td></tr>
  328. </table>
  329. </form>
  330. </body>
  331. </html>