Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.81 KB | None | 0 0
  1. <?php
  2.  
  3. if($_SERVER['REQUEST_METHOD'] == "POST"){
  4. //variables to be used from each form field's input.
  5. $ip = $_SERVER['REMOTE_ADDR'];
  6. $name = trim( filter_input( INPUT_POST, "name", FILTER_SANITIZE_STRING ) );
  7. $email = trim( filter_input( INPUT_POST, "email", FILTER_SANITIZE_EMAIL ) );
  8. $state = trim( filter_input( INPUT_POST, "state", FILTER_SANITIZE_STRING ) );
  9. $zip = trim( filter_input( INPUT_POST, "zip", FILTER_SANITIZE_STRING ) );
  10. $product_delivery = trim( filter_input( INPUT_POST, "product_delivery", FILTER_SANITIZE_STRING ) );
  11. $facility_delivery = trim( filter_input( INPUT_POST, "facility_delivery", FILTER_SANITIZE_STRING ) );
  12. $cc_cost = trim( filter_input( INPUT_POST, "cc_cost", FILTER_SANITIZE_STRING ) );
  13. $labor_cost = trim( filter_input( INPUT_POST, "labor_cost", FILTER_SANITIZE_STRING ) );
  14. $util_cost = trim( filter_input( INPUT_POST, "util_cost", FILTER_SANITIZE_STRING ) );
  15. $total_savings = ($product_delivery - $facility_delivery - $cc_cost - $labor_cost - $util_cost);
  16. $tons_per_year = trim( filter_input( INPUT_POST, "tons_per_year", FILTER_SANITIZE_STRING ) );
  17. $savings_per_year = ($total_savings * $tons_per_year);
  18.  
  19. //Blank fields cannot be submitted.
  20. if( $name == "" || $email == "" || $state == "" || $zip == "" || $product_delivery == "" || $facility_delivery == ""
  21. || $cc_cost == "" || $labor_cost == "" || $util_cost == "" || $tons_per_year == ""){
  22. $error_message = 'All fields are required! Please fill in every field.';
  23. }
  24.  
  25. //Honeypot for spam bots. if not blank, bad form input.
  26. if(!isset($error_message) && $_POST['details'] !== ""){
  27. $error_message = 'Bad form input!';
  28. }
  29.  
  30. //Adding PHPMailer
  31. require( 'phpmailer/PHPMailerAutoload.php' );
  32.  
  33. $mail = new PHPMailer;
  34.  
  35. if(!isset($error_message) && !$mail->validateAddress($email)){
  36. $error_message = 'Invalid Email Address';
  37. }
  38.  
  39. if(!isset($error_message)){
  40. //Creating the email body to be sent
  41. $email_body = "";
  42. $email_body .= "IP Address: " . $ip . "n";
  43. $email_body .= "Name: " . $name . "n";
  44. $email_body .= "Email: " . $email . "n";
  45. $email_body .= "State: " . $state . "n";
  46. $email_body .= "Zip: " . $zip . "n";
  47. $email_body .= "Costs of Product Delivery: " . $product_delivery . "n";
  48. $email_body .= "Costs of Facility Delivery: " . $facility_delivery . "n";
  49. $email_body .= "CC Costs: " . $cc_cost . "n";
  50. $email_body .= "Labor Costs: " . $labor_cost . "n";
  51. $email_body .= "Utility Costs: " . $util_cost . "n";
  52. $email_body .= "Total Savings of Installing a machine at your facility: " . $total_savings . "nn";
  53. $email_body .= "Tons per year: " . $tons_per_year . "n";
  54. $email_body .= "Savings per year: " . $savings_per_year . "n";
  55.  
  56. $mail->IsSMTP();
  57. $mail->SMTPAuth = true;
  58. $mail->Host = "mail.indcomputer.net";
  59. $mail->Port = 587;
  60. $mail->Username = "jford@indcomputer.net";
  61. $mail->Password = "122091aa";
  62. //Sending the actual email
  63. $mail->setFrom($email, $name);
  64. $mail->addAddress('jford@indcomputer.net', 'Jake Ford'); // Add a recipient
  65. $mail->isHTML(false); // Set email format to HTML
  66. $mail->Subject = 'Calculation form results from ' . $email;
  67. $mail->Body = $email_body;
  68.  
  69. if($mail->send()) {
  70. //show thank you message
  71. header('location:index.php?status=thanks');
  72. $servername = "localhost";
  73. $username = "root";
  74. $password = "";
  75. $dbname = "calculator";
  76.  
  77. try {
  78. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  79. // set the PDO error mode to exception
  80. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  81.  
  82. // prepare sql and bind parameters
  83. $query = $conn->prepare("INSERT INTO calculations (ip, name, email, state, zip, product_delivery, facility_delivery,
  84. cc_cost, labor_cost, util_cost, total_savings, tons_per_year, savings_per_year) VALUES (:ip, :name, :email, :state,
  85. :zip, :product_delivery, :facility_delivery, :cc_cost, :labor_cost, :util_cost, :total_savings, :tons_per_year, :savings_per_year)");
  86.  
  87. $query->bindParam(':ip', $ip);
  88. $query->bindParam(':name', $name);
  89. $query->bindParam(':email', $email);
  90. $query->bindParam(':state', $state);
  91. $query->bindParam(':zip', $zip);
  92. $query->bindParam(':product_delivery', $product_delivery);
  93. $query->bindParam(':facility_delivery', $facility_delivery);
  94. $query->bindParam(':cc_cost', $cc_cost);
  95. $query->bindParam(':labor_cost', $labor_cost);
  96. $query->bindParam(':util_cost', $util_cost);
  97. $query->bindParam(':total_savings', $total_savings);
  98. $query->bindParam(':tons_per_year', $tons_per_year);
  99. $query->bindParam(':savings_per_year', $savings_per_year);
  100. $query->execute();
  101. }
  102. catch(PDOException $e)
  103. {
  104. echo "Error: " . $e->getMessage();
  105. }
  106. $conn = null;
  107. exit;
  108. }
  109. $error_message = 'Message could not be sent. ';
  110. $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
  111. }
  112.  
  113. }
  114. ?>
  115. <!DOCTYPE html>
  116. <!--[if lte IE 6]><html class="preIE7 preIE8 preIE9"><![endif]-->
  117. <!--[if IE 7]><html class="preIE8 preIE9"><![endif]-->
  118. <!--[if IE 8]><html class="preIE9"><![endif]-->
  119. <!--[if gte IE 9]><!--><html><!--<![endif]-->
  120. <head>
  121. <meta charset="UTF-8">
  122. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  123. <meta name="viewport" content="width=device-width,initial-scale=1">
  124. <title>title</title>
  125. <meta name="author" content="name">
  126. <meta name="description" content="description here">
  127. <meta name="keywords" content="keywords,here">
  128. <link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon">
  129. <link rel="stylesheet" href="style.css" type="text/css">
  130. <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
  131. <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
  132. <style>
  133. body{
  134. font-family: 'Lato', sans-serif;
  135. box-sizing: border-box;
  136. }
  137.  
  138. form{
  139. background: #013C4C;
  140. width: 380px;
  141. margin: 0 auto;
  142. max-width: 480px;
  143. border: 10px solid #77BD43;
  144. border-radius: 5px;
  145. color: white;
  146. font-weight: bolder;
  147. text-align: left;
  148. padding: 10px;
  149. }
  150.  
  151. table{
  152. width: 100%;
  153. }
  154.  
  155. input:focus{
  156. background: #77BD43;
  157. }
  158.  
  159. input, select{
  160. width: 100%;
  161. }
  162.  
  163. input[type=submit]{
  164. border-radius: 5px;
  165. font-size: 1.2em;
  166. font-weight: bolder;
  167. height: 40px;
  168. margin-top: 10px;
  169. cursor: pointer;
  170. }
  171.  
  172. #failure{
  173. background-color: #F03131;
  174. color: white;
  175. border: 2px solid #fff;
  176. border-radius: 5px;
  177. padding: 5px;
  178. margin: 0;
  179. }
  180. </style>
  181. </head>
  182. <body>
  183.  
  184. <form method="post" action="index.php">
  185. <?php if(isset($_GET['status']) && $_GET['status'] == 'thanks') {
  186. echo '<p id="success">Calculation results: </p>' . $savings_per_year;
  187. } else if(isset($error_message)) {
  188. echo '<p id="failure">Error: ' . $error_message . '</p>';
  189. } else {
  190. echo "<p>Please fill out the following information below to calculate your results:</p>";
  191. }
  192. ?>
  193. <h2 style="text-align: center;">Calculator</h2>
  194. <table>
  195. <tr>
  196. <th><label for="product_delivery"> Costs of product delivery: </label></th>
  197. <td><input type="number" step="any" id="product_delivery" placeholder="REQUIRED!" name="product_delivery" value="<?php if( isset($product_delivery) ){ echo $product_delivery; } ?>" /></td>
  198. </tr>
  199. <tr>
  200. <th><label for="facility_delivery"> Costs of facility delivery: </label></th>
  201. <td><input type="number" step="any" id="facility_delivery" placeholder="REQUIRED!" name="facility_delivery" value="<?php if( isset($facility_delivery) ){ echo $facility_delivery; } ?>" /></td>
  202. </tr>
  203. <tr>
  204. <th><label for="cc_cost"> CC cost: </label></th>
  205. <td><input type="number" step="any" id="cc_cost" placeholder="REQUIRED!" name="cc_cost" value="<?php if( isset($cc_cost) ){ echo $cc_cost; } ?>" /></td>
  206. </tr>
  207. <tr>
  208. <th><label for="labor_cost"> Labor costs: </label></th>
  209. <td><input type="number" step="any" id="labor_cost" placeholder="REQUIRED!" name="labor_cost" value="<?php if( isset($labor_cost) ){ echo $labor_cost; } ?>" /></td>
  210. </tr>
  211. <tr>
  212. <th><label for="util_cost"> Utility Costs: </label></th>
  213. <td><input type="number" step="any" id="util_cost" placeholder="REQUIRED!" name="util_cost" value="<?php if( isset($util_cost) ){ echo $util_cost; } ?>" /></td>
  214. </tr>
  215. <tr>
  216. <th><label for="tons_per_year"> Tons per year: </label></th>
  217. <td><input type="number" step="any" id="tons_per_year" placeholder="REQUIRED!" name="tons_per_year" value="<?php if( isset($tons_per_year) ){ echo $tons_per_year; } ?>" /></td>
  218. </tr>
  219. <tr>
  220. <th><label for="name"> Name: </label></th>
  221. <td><input type="text" id="name" placeholder="REQUIRED!" name="name" value="<?php if( isset($name) ){ echo $name; } ?>" /></td>
  222. </tr>
  223. <tr>
  224. <th><label for="email"> Email: </label></th>
  225. <td><input type="text" id="email" placeholder="REQUIRED!" name="email" value="<?php if( isset($email) ){ echo $email; } ?>"/></td>
  226. </tr>
  227. <tr>
  228. <th><label for="state"> State: </label></th>
  229. <td>
  230. <select id="state" placeholder="REQUIRED!" name="state">
  231. <option value="AL"<?php if( isset($state) && $state == "AL" ){ echo " selected"; } ?>>Alabama</option>
  232. <option value="AK"<?php if( isset($state) && $state == "AK" ){ echo " selected"; } ?>>Alaska</option>
  233. <option value="AZ"<?php if( isset($state) && $state == "AZ" ){ echo " selected"; } ?>>Arizona</option>
  234. <option value="AR"<?php if( isset($state) && $state == "AR" ){ echo " selected"; } ?>>Arkansas</option>
  235. <option value="CA"<?php if( isset($state) && $state == "CA" ){ echo " selected"; } ?>>California</option>
  236. <option value="CO"<?php if( isset($state) && $state == "CO" ){ echo " selected"; } ?>>Colorado</option>
  237. <option value="CT"<?php if( isset($state) && $state == "CT" ){ echo " selected"; } ?>>Connecticut</option>
  238. <option value="DE"<?php if( isset($state) && $state == "DE" ){ echo " selected"; } ?>>Delaware</option>
  239. <option value="DC"<?php if( isset($state) && $state == "DC" ){ echo " selected"; } ?>>District Of Columbia</option>
  240. <option value="FL"<?php if( isset($state) && $state == "FL" ){ echo " selected"; } ?>>Florida</option>
  241. <option value="GA"<?php if( isset($state) && $state == "GA" ){ echo " selected"; } ?>>Georgia</option>
  242. <option value="HI"<?php if( isset($state) && $state == "HI" ){ echo " selected"; } ?>>Hawaii</option>
  243. <option value="ID"<?php if( isset($state) && $state == "ID" ){ echo " selected"; } ?>>Idaho</option>
  244. <option value="IL"<?php if( isset($state) && $state == "IL" ){ echo " selected"; } ?>>Illinois</option>
  245. <option value="IN"<?php if( isset($state) && $state == "IN" ){ echo " selected"; } ?>>Indiana</option>
  246. <option value="IA"<?php if( isset($state) && $state == "IA" ){ echo " selected"; } ?>>Iowa</option>
  247. <option value="KS"<?php if( isset($state) && $state == "KS" ){ echo " selected"; } ?>>Kansas</option>
  248. <option value="KY"<?php if( isset($state) && $state == "KY" ){ echo " selected"; } ?>>Kentucky</option>
  249. <option value="LA"<?php if( isset($state) && $state == "LA" ){ echo " selected"; } ?>>Louisiana</option>
  250. <option value="ME"<?php if( isset($state) && $state == "ME" ){ echo " selected"; } ?>>Maine</option>
  251. <option value="MD"<?php if( isset($state) && $state == "MD" ){ echo " selected"; } ?>>Maryland</option>
  252. <option value="MA"<?php if( isset($state) && $state == "MA" ){ echo " selected"; } ?>>Massachusetts</option>
  253. <option value="MI"<?php if( isset($state) && $state == "MI" ){ echo " selected"; } ?>>Michigan</option>
  254. <option value="MN"<?php if( isset($state) && $state == "MN" ){ echo " selected"; } ?>>Minnesota</option>
  255. <option value="MS"<?php if( isset($state) && $state == "MS" ){ echo " selected"; } ?>>Mississippi</option>
  256. <option value="MO"<?php if( isset($state) && $state == "MO" ){ echo " selected"; } ?>>Missouri</option>
  257. <option value="MT"<?php if( isset($state) && $state == "MT" ){ echo " selected"; } ?>>Montana</option>
  258. <option value="NE"<?php if( isset($state) && $state == "NE" ){ echo " selected"; } ?>>Nebraska</option>
  259. <option value="NV"<?php if( isset($state) && $state == "NV" ){ echo " selected"; } ?>>Nevada</option>
  260. <option value="NH"<?php if( isset($state) && $state == "NH" ){ echo " selected"; } ?>>New Hampshire</option>
  261. <option value="NJ"<?php if( isset($state) && $state == "NJ" ){ echo " selected"; } ?>>New Jersey</option>
  262. <option value="NM"<?php if( isset($state) && $state == "NM" ){ echo " selected"; } ?>>New Mexico</option>
  263. <option value="NY"<?php if( isset($state) && $state == "NY" ){ echo " selected"; } ?>>New York</option>
  264. <option value="NC"<?php if( isset($state) && $state == "NC" ){ echo " selected"; } ?>>North Carolina</option>
  265. <option value="ND"<?php if( isset($state) && $state == "ND" ){ echo " selected"; } ?>>North Dakota</option>
  266. <option value="OH"<?php if( isset($state) && $state == "OH" ){ echo " selected"; } ?>>Ohio</option>
  267. <option value="OK"<?php if( isset($state) && $state == "OK" ){ echo " selected"; } ?>>Oklahoma</option>
  268. <option value="OR"<?php if( isset($state) && $state == "OR" ){ echo " selected"; } ?>>Oregon</option>
  269. <option value="PA"<?php if( isset($state) && $state == "PA" ){ echo " selected"; } ?>>Pennsylvania</option>
  270. <option value="RI"<?php if( isset($state) && $state == "RI" ){ echo " selected"; } ?>>Rhode Island</option>
  271. <option value="SC"<?php if( isset($state) && $state == "SC" ){ echo " selected"; } ?>>South Carolina</option>
  272. <option value="SD"<?php if( isset($state) && $state == "SD" ){ echo " selected"; } ?>>South Dakota</option>
  273. <option value="TN"<?php if( isset($state) && $state == "TN" ){ echo " selected"; } ?>>Tennessee</option>
  274. <option value="TX"<?php if( isset($state) && $state == "TX" ){ echo " selected"; } ?>>Texas</option>
  275. <option value="UT"<?php if( isset($state) && $state == "UT" ){ echo " selected"; } ?>>Utah</option>
  276. <option value="VT"<?php if( isset($state) && $state == "VT" ){ echo " selected"; } ?>>Vermont</option>
  277. <option value="VA"<?php if( isset($state) && $state == "VA" ){ echo " selected"; } ?>>Virginia</option>
  278. <option value="WA"<?php if( isset($state) && $state == "WA" ){ echo " selected"; } ?>>Washington</option>
  279. <option value="WV"<?php if( isset($state) && $state == "WV" ){ echo " selected"; } ?>>West Virginia</option>
  280. <option value="WI"<?php if( isset($state) && $state == "WI" ){ echo " selected"; } ?>>Wisconsin</option>
  281. <option value="WY"<?php if( isset($state) && $state == "WY" ){ echo " selected"; } ?>>Wyoming</option>
  282. </select>
  283. </td>
  284. </tr>
  285. <tr>
  286. <th><label for="zip"> Zip: </label></th>
  287. <td><input type="text" id="zip" placeholder="REQUIRED!"name="zip" value="<?php if( isset($zip) ){ echo $zip; } ?>" /></td>
  288. </tr>
  289. <tr style="display: none;">
  290. <th><label for="details"></label></th>
  291. <td><input type="text" id="details" name="details" />
  292. <p>Please leave this field blank.</p>
  293. </td>
  294. </tr>
  295. </table>
  296. <input type="submit" value="Calculate!" />
  297. </form>
  298. </body>
  299. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement