Home Know Me Snaps Feedback
 
::Online Microsoft Office,Web ,Graphic and many more Tutorials
Create a PHP contact form
Creating a PHP contact form isn’t that hard, follow this tutorial and you will soon have your own contact form for your website or blog.

There are two parts to this, the form itself which will be inserted into a web page and an external PHP file which handles the data and sends it to the email address. Please note that you will need PHP installed on your web server in order for this to work. Most web servers do have PHP installed but its worth checking with your web hoster before spending your time on this.

We are going to have 4 fields which are 3 textboxes and 1 text area. The fields will be Name, Email, Subject and the text area will be for the user to add their message.

To create this form, copy and paste the following code into your webpage’s code anywhere between the <body> and </body> tags.

<form action="contact.php" method="post">
Name: <input type="text" name="name"><br/>
Email: <input type="text" name="email"><br/>
Title: <input type="text" name="title"><br/>
Message: <br/><textarea cols="40" rows="5" name="message"></textarea><br/>
<input type="submit" value="Submit Message"><br/></form>
Notice that the form action is set to contact.php, this will be the file that handles the data and sends it to your email account. So we are now going to create the contact.php page.
<?php
$to = "YOUR EMAIL ADDRESS HERE";
$subject = $_POST["title"];
$name = $_POST["name"];
$message = $_POST["message"];
$messagesent = "From: $name Message: $message";
$from = $_POST["email"];
$headers = "From: $from";
mail($to,$subject,$messagesent,$headers);
echo "Your message has been sent, thank you.";
?>

If you take a look at the code above you will notice that it defines some actions first of all, so for example it defines your email address as $to and the subject of the email as $subject. Now you will also notice a $_POST["title"]; this collects the data from the previous page in the field stated in the speech marks (in this case title). The name of the $_POST is the name of the form item.

Finally it mails the email to you by calling the $to (your email address), the $subject (title of the email), $messagesent (the message) and $headers which is the recipients name/email.

  PHP Table of Content
  Php sessions System
  Create a PHP contact form
  Comment System with php/mysql
  Admin Login and Delete Comment
  Simple file upload
  File Upload Function
  Connect with mysql database
 
   
 
 
       
   
© 2008, krishnakumar.com.np. All rights reserved
.