Skip to content

Instantly share code, notes, and snippets.

@Petah
Created November 25, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Petah/9ac4963c354d89f7c1dc to your computer and use it in GitHub Desktop.
Save Petah/9ac4963c354d89f7c1dc to your computer and use it in GitHub Desktop.
<?php
$errors = [
'name' => null,
'email' => null,
'message' => null,
];
if (!empty($_POST)) {
if (!isset($_POST['name'])) {
$errors['name'] = 'Your name is required';
} elseif (strlen($_POST['name']) > 100) {
$errors['name'] = 'Your name seems too long.';
}
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Your email address is required and must be valid';
} elseif (strlen($_POST['name']) > 200) {
$errors['name'] = 'Your email address seems too long.';
}
if (!isset($_POST['message']) || strlen($_POST['message']) < 4) {
$errors['message'] = 'A message is required and should be at least 4 letters';
} elseif (strlen($_POST['name']) > 10000) {
$errors['name'] = 'Your message seems too long.';
}
if (empty(array_filter($errors))) {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=my_db_name', 'my_db_user', 'my_db_password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8 COLLATE utf8_unicode_ci',
]);
$statement = $pdo->prepare('
INSERT INTO contact (
name,
email,
message,
created
) VALUES (
:name,
:email,
:message,
UTC_TIMESTAMP()
);
');
$result = $statement->execute([
':name' => $_POST['name'],
':email' => $_POST['email'],
':message' => $_POST['message'],
]);
$message = '';
$message = 'Name: ' . $_POST['name'] . PHP_EOL;
$message = 'Email: ' . $_POST['email'] . PHP_EOL;
$message = 'Message: ' . $_POST['message'] . PHP_EOL;
mail('website.owner@example.com', 'New contact message', $message, "From: website@example.com\r\nReply-To: {$_POST['email']}");
if ($result) {
header('Location: success.php');
return;
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Contact Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="" method="post">
<label for="name">Name</label>
<input name="name" id="name" required maxlength="100" value="<?= isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES): ''; ?>">
<div><?= $errors['name']; ?></div>
<label for="email">Email Address</label>
<input name="email" id="email" type="email" required maxlength="200" value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES): ''; ?>">
<div><?= $errors['email']; ?></div>
<label for="message">Message</label>
<textarea name="message" id="message" required maxlength="10000" minlength="4"><?= isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''; ?></textarea>
<div><?= $errors['message']; ?></div>
<button>Submit</button>
</form>
</body>
</html>
DROP DATABASE IF EXISTS my_db_name;
DROP USER 'my_db_user'@'localhost';
CREATE DATABASE IF NOT EXISTS my_db_name
DEFAULT CHARSET = utf8
COLLATE = utf8_unicode_ci;
CREATE USER 'my_db_user'@'localhost'
IDENTIFIED BY 'my_db_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON my_db_name.* TO 'my_db_user'@'localhost';
USE my_db_name;
CREATE TABLE IF NOT EXISTS contact (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(200) NOT NULL,
message TEXT NOT NULL,
created DATETIME NOT NULL
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_unicode_ci;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Thanks</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
Thanks for contacting us!
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment