Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 12, 2023 13:44
Show Gist options
  • Save code-boxx/d39f2ed8145977810f2c17d5baef4a2b to your computer and use it in GitHub Desktop.
Save code-boxx/d39f2ed8145977810f2c17d5baef4a2b to your computer and use it in GitHub Desktop.
PHP MYSQL Order Form

PHP MYSQL ORDER FORM

https://code-boxx.com/simple-php-order-form/

IMAGES

order

NOTES

  1. Create a test database and import 1-orders.sql.
  2. Change the database settings in 3-process.php to your own.
  3. Launch 2-order-form.php in the browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `orders` (
`order_id` bigint(20) NOT NULL,
`dop` datetime NOT NULL DEFAULT current_timestamp(),
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`qty` int(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `orders`
ADD PRIMARY KEY (`order_id`),
ADD KEY `dop` (`dop`);
ALTER TABLE `orders`
MODIFY `order_id` bigint(20) NOT NULL AUTO_INCREMENT;
<!DOCTYPE html>
<html>
<head>
<title>Order Form Example</title>
<meta charset="utf-8">
<link href="x-cosmetics.css" rel="stylesheet">
</head>
<body>
<?php
// (A) PROCESS ORDER FORM
if (isset($_POST["name"])) {
require "3-process.php";
echo $result == ""
? "<div class='notify'>Thank You! We have received your order.</div>"
: "<div class='notify'>$result</div>" ;
}
?>
<!-- (B) ORDER FORM -->
<div id="form-wrap">
<img id="form-img" src="order.png">
<form id="form-order" method="post" target="_self">
<label>Name</label>
<input type="text" name="name" required value="Jon Doe">
<label>Email</label>
<input type="email" name="email" required value="[email protected]">
<label>Quantity</label>
<input type="number" name="qty" required min="1" max="99" value="1">
<input type="submit" value="Place Order">
</form>
</div>
</body>
</html>
<?php
// (A) PROCESS RESULT
$result = "";
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
$dbhost = "localhost";
$dbname = "test";
$dbchar = "utf8mb4";
$dbuser = "root";
$dbpass = "";
$pdo = new PDO(
"mysql:host=$dbhost;dbname=$dbname;charset=$dbchar",
$dbuser, $dbpass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) SAVE ORDER TO DATABASE
if ($result=="") { try {
$stmt = $pdo->prepare("INSERT INTO `orders` (`name`, `email`, `qty`) VALUES (?,?,?)");
$stmt->execute([$_POST["name"], $_POST["email"], $_POST["qty"]]);
} catch (Exception $ex) { $result = $ex->getMessage(); }}
// (D) SEND ORDER VIA EMAIL (OPTIONAL)
if ($result=="") {
$to = "[email protected]"; // CHANGE TO YOUR OWN!
$subject = "ORDER RECEIVED";
$message = "";
foreach ($_POST as $k=>$v) { $message .= "$k - $v\r\n"; }
if (!@mail($to, $subject, $message)) { $result = "Error sending mail!"; }
}
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) NOTIFICATIONS */
div.notify {
background: #fff7ce;
padding: 10px;
margin-bottom: 20px;
font-weight: 700;
}
/* (C) ORDER FORM */
#form-wrap { display: flex; }
#form-img { max-width: 200px; object-fit: cover; }
#form-order {
padding: 20px;
border: 1px solid #efefef;
background: #f5f5f5;
}
#form-order label, #form-order input {
display: block;
width: 100%;
}
#form-order label { margin: 10px 0; }
#form-order label:first-child { margin-top: 0; }
#form-order input {
font-size: 1em;
border: 1px solid #e3e3e3;
padding: 10px;
}
#form-order input[type=submit] {
margin-top: 30px;
border: 0;
background: #bf3434;
color: #fff;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment