Create a new PHP file. Let's say it's named
message.php. Now, move on to your other file that contains the HTML form, which we'll call
message.html.
For the HTML file,
message.html, you're going to want your form fields acquire a
name attribute and your form itself will need the
action attribute and
method attribute. So:
CODE
<form action="message.php" method="post">
<table>
<tr>
<td><label for="name">Name:</label></td>
<td><input id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email">E-mail:</label></td>
<td><input id="email" name="email" /></td>
</tr>
<tr>
<td><label for="message">Message:</label></td>
<td><textarea id="message" name="message"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Message" /></td>
</tr>
</table>
</form>
You see how the
action attribute of the form links to the PHP file? Basically, it runs whatever is in that PHP file for the form. And the
method attribute uses the
post method, which means it is being submitted. And for all the form fields, the
name attribute contains the value of the field so that when it is posted, the PHP can recognize it.
Now, let's move on to the PHP,
message.php:
CODE
<?php
// Retrieve the posted content from the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Now, submit the form if the $_POST array isn't empty. However, since PHP5, an empty array still counts as '1'. So, we have adjust this issue accordingly.
if (count($_POST) >= 2) {
// Check to see if there are any empty fields, though. If there are, generate an error.
if (!$name || !$email || !$message) echo 'Please fill in all fields.';
// Since this demonstration form submits an e-mail address, we need to check to see if the e-mail address is valid.
else if (!preg_match('~^.+?@[\w\.-]+\.[a-z]{2,4}$~', $email)) echo 'Use a valid e-mail address.';
// If everything checks out alright, continue.
else {
$to = 'youremail@address.com';
$subject = 'New message!';
$message = 'You have received a new message from ' . $name . '.
"' . $message . '"';
$from = 'From: ' . $email;
mail($to, $subject, $message, $from);
}
}
?>
I attached a few comments to help you out. But, basically, we take what was submitted from the form and assign those values to some variables. From there, we just count how many fields were posted (in this case, greater than or equal to two). If that's true, we go on to check if any one of those fields are empty (or null, using the
! operator). We also check for certain characters and symbols in the e-mail field to see if it's a valid email address (
name@domain.com). After that, if there are no errors, we assign the e-mail address you want use to receive the messages (or details, whatever) to a variable, as well as a subject, a message, and a
From header. We use the variables we got from the submitted data to fill in some of the details and then we use the
mail() function (which is a built-in PHP function, really lovely) to send an e-mail to the supplied e-mail address. From there, you just check your e-mail and read the message generated by the PHP file.
A bit confusing at first, yes, but it's pretty easy to learn. Especially with an issue like this. You can also use this kind of PHP to update MySQL databases, if that's what you want. But, let me know if you need any help with this.