Wednesday, 30 January 2013

Simple Contact Us Form in Php | PHP Tutorials for Beginners


Contact or Feedback Form in PHP:
Contact form is very essential for any web site. You can find every website have contact us form. With Contact us form or feedback form the owner of site comes to know what people think about there site. Php contact form is very easy to create. In this tutorial of php code for beginners I will show you the step by step procedure to create your contact us form in php.

First we create a html page for our contact us form. Copy the below code and save as contact.php/ contact.html.

<html>
<body>
<table>
<form name="contact_form" method="post" action="php_contact_form.php">
<tr>
<td><b>PHP Contact Form</b></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="sub" id="sub" type="text" size="75"></td>
</tr>
<tr>
<td>Name:</td>
<td><input name="name" id="name" type="text"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" id="email" type="text"></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="msg" cols="60" rows="5" id="msg"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit">
</tr>
</form>
</table>
</body>
</html>

In above code we have created contact us fields for the end user. As this is Simple contact form in php hence i used only name, subject,email and message fields. You can add more. After clicking submit button "php_contact_us.php" page will be called. This is the php contact page where we fetch the data of contact.html and send it using php mail function.

Now we will create "php_contact_us.php" page. In this page we write our php coding to send contact/feedback mail to admin of website.Copy the below code and save as .php_contact_us.php".

<?php
$subject = $_POST['sub'];
$name = $_POST['name'];
$from = $_POST['email'];
$message = $_POST['msg'];
$to = "[email protected]";
$send_email = mail($to,$subject,$message);
if($send_email){
echo "Your message is successfully sent!!!";
}else{
echo "Error in sending contact email!!!";
}
?>

In above code we use $subject to store subject of contact us form. Similarly we have used $name for the Senders name, $from for storing sender email address, $message for you message of feedback, and very important $to where receivers address is set.
After storing this data into variable now we have used php mail function to send the mail. After sending mail we are checking is mail sent proper or not. If it is successfully sent then we will show greeting message other wise some error message.

1 comment:

  1. I've created a really similar tutorial but for people who require the "mail class" on their hosting (i.e. Arvixe users). Check it out on the blorg!

    http://codetutorial.caroshaw.com/contact_form_php_arvixe.php

    ReplyDelete