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.
<form name=”contact_form” method=”post” action=”php_contact_form.php”>
<td><b>PHP Contact Form</b></td>
<td><input name=”sub” id=”sub” type=”text” size=”75″></td>
<td><input name=”name” id=”name” type=”text”></td>
<td><input name=”email” id=”email” type=”text”></td>
<td><textarea name=”msg” cols=”60″ rows=”5″ id=”msg”></textarea></td>
<td><input type=”submit” name=”Submit” value=”Submit”>
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”.
$subject = $_POST['sub'];
$name = $_POST['name'];
$from = $_POST['email'];
$message = $_POST['msg'];
$to = “firstname.lastname@example.org”;
$send_email = mail($to,$subject,$message);
echo “Your message is successfully sent!!!”;
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.