Posts Tagged ‘forms’
Simple Form Handling with PHP
Saturday, October 25th, 2008
I’ve seen some requests lately on how to handle form data. It’s pretty easy, and here is the most basic way of doing it. There isn’t any validation, or styling with CSS - just three separate files. You could actually do this with a little javascript/css and some php and do everything in one file - but like I said, this is the most basic way of doing it.
Your HTML Form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
“http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<title>A simple form</title>
</head>
<body>
<h1>A form</h1>
<form action=”mail.php” method=”post”>
<label for=”theName”>Name:</label><br>
<input type=”text” name=”name” id=”theName”><br>
<label for=”theEmail”>Email Address:</label><br>
<input type=”text” name=”email” id=”theEmail”><br>
<label for=”theMessage”>Message:</label><br>
<textarea name=”message” id=”theMessage”></textarea><br>
<input type=”submit” value=”send”>
</form>
</body>
</html>
Your php file
<?php
//enter the address to send the form to
$toAddress = “someone@somewhere.com”;
//enter the subject of the email you recieve
$subject = “Contact from Web”;
//enter the path to the file where the user gets sent to after submitting the form
$thankYou = “thanks.php”;
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$body = “
$name
$email
$message”;
mail($toAddress, $subject, $body, “From: $email”);
header(”Location: $thankYou”);
?>

