Online: 103   Members: 4,232

Creating A Contact Form

So, you have a site and rather than put your email everywhere, you want to have a contact form. Well first lets start with the form itself.

The basic elements for a contact form are:

  • Name
  • Senders Email
  • Subject
  • Body

So lets make a little form with them on it:

<form action="email.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Subject: <input type="text" name="subject"><br>
Body: <textarea cols="65" rows="15" name="body"></textarea><br>
<input type="submit" name="email" value="Email"> 
<input type="reset" name="reset" value="Reset"><br>

Well that is the form, now time for email.php. First we want to see if whoever sent the email included all the fields so we'll have the script create a variable if there is a blank field:


	$error.="You didn't fill in the name field. Please go back and fill it in.";
	$error.="You didn't fill in the email field. Please go back and fill it in.";
	$error.="You didn't fill in the subject field. Please go back and fill it in.";
	$error.="You didn't fill in the body field. Please go back and fill it in.";

Ok now we'll see if the script has made the variable $error. If it has, it will say the error and not send the form, but if not it will continue with emailing the form to your email address. I will include a variable for you to fill out to set to your email.

$youremail = "Your Email Here";

  echo "<b>$error</b>";
} else {
  mail("$youremail","$subject","$body","From: $email");
  echo "Your email has been sent.";

Well that's it! Now when the user sends the email you will receive the email from them (instead of your site).

Related Tutorials

Useful Resources