Basic Form to Email PHP Contact Form
When I created my first website with contact form I discovered I can’t send any email because there was no additional functionality added to contact form.
Here is how easly and fast you can fix it by adding below code. There is no additional css so u can change “the cosmetics” as u wish.
HTML file:
File Name: contactform.htm (you can change the filename to anything you like).
<form name=”contactform” method=”post” action=”send_form_email.php”> <table width=”450px”> <tr> <td valign=”top”> <label for=”first_name”>First Name *</label> </td> <td valign=”top”> <input type=”text” name=”first_name” maxlength=”50″ size=”30″> </td> </tr> <tr> <td valign=”top””> <label for=”last_name”>Last Name *</label> </td> <td valign=”top”> <input type=”text” name=”last_name” maxlength=”50″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”email”>Email Address *</label> </td> <td valign=”top”> <input type=”text” name=”email” maxlength=”80″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”telephone”>Telephone Number</label> </td> <td valign=”top”> <input type=”text” name=”telephone” maxlength=”30″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”comments”>Comments *</label> </td> <td valign=”top”> <textarea name=”comments” maxlength=”1000″ cols=”25″ rows=”6″></textarea> </td> </tr> </table> </form> |
|
Below the PHP file which is ur functionality ( catches the html form fields which in our case is first_name, last_name, email, phone and comments and sends them via email using POST method).
File Name: send_form_email.php ( you have to keep exactly the same name )
<?php
if(isset($_POST[’email’])) { // CHANGE THE TWO LINES BELOW $email_subject = “website messages”; function died($error) { // validation expected data exists $first_name = $_POST[‘first_name’]; // required $error_message = “”; function clean_string($string) { $email_message .= “First Name: “.clean_string($first_name).”\n”; // create email headers ?> <!– place your own success html below –> Thank you for contacting us. I will be in touch with you very soon. <?php die(); |
After saving this two files ( html and php ) u are ready to go.
This is how your form should looks like.
If you want to dig more into PHP files u can also notice form validations like email has to contain @ sign or first and last name supposed to contain letters not numbers which is indicated by adding a string.
You can also notice * ( astrix sign ) which marks the fields which has to be filled in form ( in our case all except telephone number ).
Please consider spam prevention because this is just simple contact form. To make your contact form more secure have a look at my tutorial Php captcha contact form.
Hope you learned something new
If you learned something new from this tutorial or if you have any question let me know in comments section.