Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if($_SERVER['REQUEST_METHOD'] == "POST"){
- //variables to be used from each form field's input.
- $ip = $_SERVER['REMOTE_ADDR'];
- $name = trim( filter_input( INPUT_POST, "name", FILTER_SANITIZE_STRING ) );
- $email = trim( filter_input( INPUT_POST, "email", FILTER_SANITIZE_EMAIL ) );
- $state = trim( filter_input( INPUT_POST, "state", FILTER_SANITIZE_STRING ) );
- $zip = trim( filter_input( INPUT_POST, "zip", FILTER_SANITIZE_STRING ) );
- $product_delivery = trim( filter_input( INPUT_POST, "product_delivery", FILTER_SANITIZE_STRING ) );
- $facility_delivery = trim( filter_input( INPUT_POST, "facility_delivery", FILTER_SANITIZE_STRING ) );
- $cc_cost = trim( filter_input( INPUT_POST, "cc_cost", FILTER_SANITIZE_STRING ) );
- $labor_cost = trim( filter_input( INPUT_POST, "labor_cost", FILTER_SANITIZE_STRING ) );
- $util_cost = trim( filter_input( INPUT_POST, "util_cost", FILTER_SANITIZE_STRING ) );
- $total_savings = ($product_delivery - $facility_delivery - $cc_cost - $labor_cost - $util_cost);
- $tons_per_year = trim( filter_input( INPUT_POST, "tons_per_year", FILTER_SANITIZE_STRING ) );
- $savings_per_year = ($total_savings * $tons_per_year);
- //Blank fields cannot be submitted.
- if( $name == "" || $email == "" || $state == "" || $zip == "" || $product_delivery == "" || $facility_delivery == ""
- || $cc_cost == "" || $labor_cost == "" || $util_cost == "" || $tons_per_year == ""){
- $error_message = 'All fields are required! Please fill in every field.';
- }
- //Honeypot for spam bots. if not blank, bad form input.
- if(!isset($error_message) && $_POST['details'] !== ""){
- $error_message = 'Bad form input!';
- }
- //Adding PHPMailer
- require( 'phpmailer/PHPMailerAutoload.php' );
- $mail = new PHPMailer;
- if(!isset($error_message) && !$mail->validateAddress($email)){
- $error_message = 'Invalid Email Address';
- }
- if(!isset($error_message)){
- //Creating the email body to be sent
- $email_body = "";
- $email_body .= "IP Address: " . $ip . "n";
- $email_body .= "Name: " . $name . "n";
- $email_body .= "Email: " . $email . "n";
- $email_body .= "State: " . $state . "n";
- $email_body .= "Zip: " . $zip . "n";
- $email_body .= "Costs of Product Delivery: " . $product_delivery . "n";
- $email_body .= "Costs of Facility Delivery: " . $facility_delivery . "n";
- $email_body .= "CC Costs: " . $cc_cost . "n";
- $email_body .= "Labor Costs: " . $labor_cost . "n";
- $email_body .= "Utility Costs: " . $util_cost . "n";
- $email_body .= "Total Savings of Installing a machine at your facility: " . $total_savings . "nn";
- $email_body .= "Tons per year: " . $tons_per_year . "n";
- $email_body .= "Savings per year: " . $savings_per_year . "n";
- $mail->IsSMTP();
- $mail->SMTPAuth = true;
- $mail->Host = "mail.indcomputer.net";
- $mail->Port = 587;
- $mail->Username = "jford@indcomputer.net";
- $mail->Password = "122091aa";
- //Sending the actual email
- $mail->setFrom($email, $name);
- $mail->addAddress('jford@indcomputer.net', 'Jake Ford'); // Add a recipient
- $mail->isHTML(false); // Set email format to HTML
- $mail->Subject = 'Calculation form results from ' . $email;
- $mail->Body = $email_body;
- if($mail->send()) {
- //show thank you message
- header('location:index.php?status=thanks');
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "calculator";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // prepare sql and bind parameters
- $query = $conn->prepare("INSERT INTO calculations (ip, name, email, state, zip, product_delivery, facility_delivery,
- cc_cost, labor_cost, util_cost, total_savings, tons_per_year, savings_per_year) VALUES (:ip, :name, :email, :state,
- :zip, :product_delivery, :facility_delivery, :cc_cost, :labor_cost, :util_cost, :total_savings, :tons_per_year, :savings_per_year)");
- $query->bindParam(':ip', $ip);
- $query->bindParam(':name', $name);
- $query->bindParam(':email', $email);
- $query->bindParam(':state', $state);
- $query->bindParam(':zip', $zip);
- $query->bindParam(':product_delivery', $product_delivery);
- $query->bindParam(':facility_delivery', $facility_delivery);
- $query->bindParam(':cc_cost', $cc_cost);
- $query->bindParam(':labor_cost', $labor_cost);
- $query->bindParam(':util_cost', $util_cost);
- $query->bindParam(':total_savings', $total_savings);
- $query->bindParam(':tons_per_year', $tons_per_year);
- $query->bindParam(':savings_per_year', $savings_per_year);
- $query->execute();
- }
- catch(PDOException $e)
- {
- echo "Error: " . $e->getMessage();
- }
- $conn = null;
- exit;
- }
- $error_message = 'Message could not be sent. ';
- $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
- }
- }
- ?>
- <!DOCTYPE html>
- <!--[if lte IE 6]><html class="preIE7 preIE8 preIE9"><![endif]-->
- <!--[if IE 7]><html class="preIE8 preIE9"><![endif]-->
- <!--[if IE 8]><html class="preIE9"><![endif]-->
- <!--[if gte IE 9]><!--><html><!--<![endif]-->
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <title>title</title>
- <meta name="author" content="name">
- <meta name="description" content="description here">
- <meta name="keywords" content="keywords,here">
- <link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon">
- <link rel="stylesheet" href="style.css" type="text/css">
- <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
- <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
- <style>
- body{
- font-family: 'Lato', sans-serif;
- box-sizing: border-box;
- }
- form{
- background: #013C4C;
- width: 380px;
- margin: 0 auto;
- max-width: 480px;
- border: 10px solid #77BD43;
- border-radius: 5px;
- color: white;
- font-weight: bolder;
- text-align: left;
- padding: 10px;
- }
- table{
- width: 100%;
- }
- input:focus{
- background: #77BD43;
- }
- input, select{
- width: 100%;
- }
- input[type=submit]{
- border-radius: 5px;
- font-size: 1.2em;
- font-weight: bolder;
- height: 40px;
- margin-top: 10px;
- cursor: pointer;
- }
- #failure{
- background-color: #F03131;
- color: white;
- border: 2px solid #fff;
- border-radius: 5px;
- padding: 5px;
- margin: 0;
- }
- </style>
- </head>
- <body>
- <form method="post" action="index.php">
- <?php if(isset($_GET['status']) && $_GET['status'] == 'thanks') {
- echo '<p id="success">Calculation results: </p>' . $savings_per_year;
- } else if(isset($error_message)) {
- echo '<p id="failure">Error: ' . $error_message . '</p>';
- } else {
- echo "<p>Please fill out the following information below to calculate your results:</p>";
- }
- ?>
- <h2 style="text-align: center;">Calculator</h2>
- <table>
- <tr>
- <th><label for="product_delivery"> Costs of product delivery: </label></th>
- <td><input type="number" step="any" id="product_delivery" placeholder="REQUIRED!" name="product_delivery" value="<?php if( isset($product_delivery) ){ echo $product_delivery; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="facility_delivery"> Costs of facility delivery: </label></th>
- <td><input type="number" step="any" id="facility_delivery" placeholder="REQUIRED!" name="facility_delivery" value="<?php if( isset($facility_delivery) ){ echo $facility_delivery; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="cc_cost"> CC cost: </label></th>
- <td><input type="number" step="any" id="cc_cost" placeholder="REQUIRED!" name="cc_cost" value="<?php if( isset($cc_cost) ){ echo $cc_cost; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="labor_cost"> Labor costs: </label></th>
- <td><input type="number" step="any" id="labor_cost" placeholder="REQUIRED!" name="labor_cost" value="<?php if( isset($labor_cost) ){ echo $labor_cost; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="util_cost"> Utility Costs: </label></th>
- <td><input type="number" step="any" id="util_cost" placeholder="REQUIRED!" name="util_cost" value="<?php if( isset($util_cost) ){ echo $util_cost; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="tons_per_year"> Tons per year: </label></th>
- <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>
- </tr>
- <tr>
- <th><label for="name"> Name: </label></th>
- <td><input type="text" id="name" placeholder="REQUIRED!" name="name" value="<?php if( isset($name) ){ echo $name; } ?>" /></td>
- </tr>
- <tr>
- <th><label for="email"> Email: </label></th>
- <td><input type="text" id="email" placeholder="REQUIRED!" name="email" value="<?php if( isset($email) ){ echo $email; } ?>"/></td>
- </tr>
- <tr>
- <th><label for="state"> State: </label></th>
- <td>
- <select id="state" placeholder="REQUIRED!" name="state">
- <option value="AL"<?php if( isset($state) && $state == "AL" ){ echo " selected"; } ?>>Alabama</option>
- <option value="AK"<?php if( isset($state) && $state == "AK" ){ echo " selected"; } ?>>Alaska</option>
- <option value="AZ"<?php if( isset($state) && $state == "AZ" ){ echo " selected"; } ?>>Arizona</option>
- <option value="AR"<?php if( isset($state) && $state == "AR" ){ echo " selected"; } ?>>Arkansas</option>
- <option value="CA"<?php if( isset($state) && $state == "CA" ){ echo " selected"; } ?>>California</option>
- <option value="CO"<?php if( isset($state) && $state == "CO" ){ echo " selected"; } ?>>Colorado</option>
- <option value="CT"<?php if( isset($state) && $state == "CT" ){ echo " selected"; } ?>>Connecticut</option>
- <option value="DE"<?php if( isset($state) && $state == "DE" ){ echo " selected"; } ?>>Delaware</option>
- <option value="DC"<?php if( isset($state) && $state == "DC" ){ echo " selected"; } ?>>District Of Columbia</option>
- <option value="FL"<?php if( isset($state) && $state == "FL" ){ echo " selected"; } ?>>Florida</option>
- <option value="GA"<?php if( isset($state) && $state == "GA" ){ echo " selected"; } ?>>Georgia</option>
- <option value="HI"<?php if( isset($state) && $state == "HI" ){ echo " selected"; } ?>>Hawaii</option>
- <option value="ID"<?php if( isset($state) && $state == "ID" ){ echo " selected"; } ?>>Idaho</option>
- <option value="IL"<?php if( isset($state) && $state == "IL" ){ echo " selected"; } ?>>Illinois</option>
- <option value="IN"<?php if( isset($state) && $state == "IN" ){ echo " selected"; } ?>>Indiana</option>
- <option value="IA"<?php if( isset($state) && $state == "IA" ){ echo " selected"; } ?>>Iowa</option>
- <option value="KS"<?php if( isset($state) && $state == "KS" ){ echo " selected"; } ?>>Kansas</option>
- <option value="KY"<?php if( isset($state) && $state == "KY" ){ echo " selected"; } ?>>Kentucky</option>
- <option value="LA"<?php if( isset($state) && $state == "LA" ){ echo " selected"; } ?>>Louisiana</option>
- <option value="ME"<?php if( isset($state) && $state == "ME" ){ echo " selected"; } ?>>Maine</option>
- <option value="MD"<?php if( isset($state) && $state == "MD" ){ echo " selected"; } ?>>Maryland</option>
- <option value="MA"<?php if( isset($state) && $state == "MA" ){ echo " selected"; } ?>>Massachusetts</option>
- <option value="MI"<?php if( isset($state) && $state == "MI" ){ echo " selected"; } ?>>Michigan</option>
- <option value="MN"<?php if( isset($state) && $state == "MN" ){ echo " selected"; } ?>>Minnesota</option>
- <option value="MS"<?php if( isset($state) && $state == "MS" ){ echo " selected"; } ?>>Mississippi</option>
- <option value="MO"<?php if( isset($state) && $state == "MO" ){ echo " selected"; } ?>>Missouri</option>
- <option value="MT"<?php if( isset($state) && $state == "MT" ){ echo " selected"; } ?>>Montana</option>
- <option value="NE"<?php if( isset($state) && $state == "NE" ){ echo " selected"; } ?>>Nebraska</option>
- <option value="NV"<?php if( isset($state) && $state == "NV" ){ echo " selected"; } ?>>Nevada</option>
- <option value="NH"<?php if( isset($state) && $state == "NH" ){ echo " selected"; } ?>>New Hampshire</option>
- <option value="NJ"<?php if( isset($state) && $state == "NJ" ){ echo " selected"; } ?>>New Jersey</option>
- <option value="NM"<?php if( isset($state) && $state == "NM" ){ echo " selected"; } ?>>New Mexico</option>
- <option value="NY"<?php if( isset($state) && $state == "NY" ){ echo " selected"; } ?>>New York</option>
- <option value="NC"<?php if( isset($state) && $state == "NC" ){ echo " selected"; } ?>>North Carolina</option>
- <option value="ND"<?php if( isset($state) && $state == "ND" ){ echo " selected"; } ?>>North Dakota</option>
- <option value="OH"<?php if( isset($state) && $state == "OH" ){ echo " selected"; } ?>>Ohio</option>
- <option value="OK"<?php if( isset($state) && $state == "OK" ){ echo " selected"; } ?>>Oklahoma</option>
- <option value="OR"<?php if( isset($state) && $state == "OR" ){ echo " selected"; } ?>>Oregon</option>
- <option value="PA"<?php if( isset($state) && $state == "PA" ){ echo " selected"; } ?>>Pennsylvania</option>
- <option value="RI"<?php if( isset($state) && $state == "RI" ){ echo " selected"; } ?>>Rhode Island</option>
- <option value="SC"<?php if( isset($state) && $state == "SC" ){ echo " selected"; } ?>>South Carolina</option>
- <option value="SD"<?php if( isset($state) && $state == "SD" ){ echo " selected"; } ?>>South Dakota</option>
- <option value="TN"<?php if( isset($state) && $state == "TN" ){ echo " selected"; } ?>>Tennessee</option>
- <option value="TX"<?php if( isset($state) && $state == "TX" ){ echo " selected"; } ?>>Texas</option>
- <option value="UT"<?php if( isset($state) && $state == "UT" ){ echo " selected"; } ?>>Utah</option>
- <option value="VT"<?php if( isset($state) && $state == "VT" ){ echo " selected"; } ?>>Vermont</option>
- <option value="VA"<?php if( isset($state) && $state == "VA" ){ echo " selected"; } ?>>Virginia</option>
- <option value="WA"<?php if( isset($state) && $state == "WA" ){ echo " selected"; } ?>>Washington</option>
- <option value="WV"<?php if( isset($state) && $state == "WV" ){ echo " selected"; } ?>>West Virginia</option>
- <option value="WI"<?php if( isset($state) && $state == "WI" ){ echo " selected"; } ?>>Wisconsin</option>
- <option value="WY"<?php if( isset($state) && $state == "WY" ){ echo " selected"; } ?>>Wyoming</option>
- </select>
- </td>
- </tr>
- <tr>
- <th><label for="zip"> Zip: </label></th>
- <td><input type="text" id="zip" placeholder="REQUIRED!"name="zip" value="<?php if( isset($zip) ){ echo $zip; } ?>" /></td>
- </tr>
- <tr style="display: none;">
- <th><label for="details"></label></th>
- <td><input type="text" id="details" name="details" />
- <p>Please leave this field blank.</p>
- </td>
- </tr>
- </table>
- <input type="submit" value="Calculate!" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement