Skip to content

Instantly share code, notes, and snippets.

@mcmullengreg
Last active August 29, 2015 14:26
Show Gist options
  • Save mcmullengreg/4ff0a64b23557d9554c7 to your computer and use it in GitHub Desktop.
Save mcmullengreg/4ff0a64b23557d9554c7 to your computer and use it in GitHub Desktop.
Simple $_POST Email Form. This form does not contain any form validation and has very little security checking on the backend and nothing on the front end.
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Basic PHP Form Processing</title>
</head>
<body>
<h1>Basic Form</h1>
<?php if (empty($_POST)) : ?>
<form action="test.php" method="post"><!-- can also use "get", get passes data through the URL. -->
<p>
<label for="name">Your Name</label>
<input type="text" name="name" value="<?php echo (isset($_POST['name']) ? $_POST['name'] : '' ); ?>" placeholder="" />
</p>
<p>
<label for="poNumber">P.O. Number</label>
<input type="text" name="poNumber" value="<?php echo (isset($_POST['poNumber']) ? $_POST['poNumber'] : '' ); ?>" placeholder="" />
</p>
<p>
<label for="item1">Item 1</label>
<input type="text" name="item1" value="<?php echo (isset($_POST['item1']) ? $_POST['item1'] : '' ); ?>" placeholder="" />
</p>
<p>
<label for="item2">Item 2</label>
<input type="text" name="item2" value="<?php echo (isset($_POST['item2']) ? $_POST['item2'] : '' ); ?>" placeholder="" />
</p>
<p><input type="submit" value="Whatever Submit text you want" /></p>
</form>
<?php else : ?>
<?php
// This content is all you need if posting to another page.
// To check for a non empty $_POST use:
// if (!empty($_POST)) { EVERYTHING UNTIL THE MAIL }
// See if a value exists, trim and strip tags
// $_POST[''] value should match the name of the field
$name = (isset($_POST['name']) ? strip_tags(trim($_POST['name'])) : '');
$poNumber = (isset($_POST['poNumber']) ? strip_tags(trim($_POST['poNumber'])) : '');
$item1 = (isset($_POST['item1']) ? strip_tags(trim($_POST['item1'])) : '');
$item2 = (isset($_POST['item2']) ? strip_tags(trim($_POST['item2'])) : '');
// Build the message
$message = '<table style="border:solid 1px #000; border-collapse:collapse;">'
. '<tr>'
. '<td><strong>Name: </strong></td>'
. '<td>'.$name.'</td>'
. '</tr>'
. '<tr>'
. '<td><strong>PO Number: </strong></td>'
. '<td>'.$poNumber.'</td>'
. '</tr>'
. '<tr>'
. '<td><strong>Item 1: </strong></td>'
. '<td>'.$item1.'</td>'
. '</tr>'
. '<tr>'
. '<td><strong>Item 2: </strong></td>'
. '<td>'.$item2.'</td>'
. '</tr>'
. '</table>';
// Set the "to address"
$to = "whoever@whatever.com";
// Set the subject
$subject = "Simple Form";
// Don't mess with the spacing here. It'll break. (It's called EOF, if you want to look it up.
// Just adjust the FROM
$headers = <<<MESSAGE
From: WHOEVER <thatGuy@WHATEVEr.com>
Content-Type: text/html; charset=iso-8859-1;
MESSAGE;
// This will email the data.
mail($to, $subject, $message, $headers);
?>
<?php
/* If you want to see the data
Remove me before going into production
print_r($_POST);
*/
?>
<?php endif; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment