PHP Contact Form
fleurchild.com > Resources > Tutorials > PHP Contact Form
We are going to make a simple PHP Conact Form in this tutorial. The form will have a NAME, E-MAIL, and MESSAGE textarea.
PHP
Let's start off by creating the areas seen by the resulting textareas in HTML. As you can see, the NAME, E-MAIL, and MESSAGE textarea are accounted for. The areas in UPPERCASE is to be edited by you.
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "YOUR E-MAIL ADDRESS HERE";
$Subject = "E-MAIL SUBJECT";
$Name = Trim(stripslashes($_POST['Name']));
$Message = Trim(stripslashes($_POST['Message']));
Next, we move onto the validation codes:
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;
}
The rest of the codes refer to the body text that will be sent in the e-mail. Also, they consider the resultant pages if the e-mail is sent or not sent properly.
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
In Conclusion
Once you have saved this the codes into one file called form.php, you may move on to the HTML component of the tutorial.
HTML
Create the success.htm and error.htm pages. The success page will simply say that the message has been sent, and include a back link to the previous page. On the other hand, the error page will say that the message has not been sent, and include a back link to the previous page suggesting to try sending the message again.
Copy and paste the form below on the page you would like to have display the form:
<br>E-mail:</br>
<br><input type="text" name="EmailFrom"></br>
<br>Name:
<br><input type="text" name="Name"></br>
<br>Message:</br>
<br><input type="text" name="Message"><br /></p>
<br><input type="submit" name="submit" value="Submit"></br>
</form>
And you're done! The form should be functional now!
to fleurchild.com, Julia's little corner of the webernet. Be sure to check out the many high-quality resources the site has made availible for you. If you have a request of any kind, don't be afraid to from me a message in the blog. Back to the Top.
