A contact form is something that everyone needs every now and then. Be it a blog or a website that you run, you might always need to use a contact form there. So, here I am to the rescue, to teach you how to create a Contact from using PHP in a very simple way. Before you proceed with creation of this contact form, I assume that you have prior knowledge of working with HTML forms and some basic PHP knowledge.
Creation of a form includes 2 side scripting, the client side scripting (which will handle how the user or the client sees the contact form) and the server side scripting (which will handle the sending of the mail part). The client side code I use here is a pretty basic and all is without CSS formatting and styling. Feel free to style the client side script in whichever way you want and use it wherever you want! In order to code this tutorial in an efficient way, I suggest you to get Notepad++. Read now why you should use that!
This is the thing that we are going to create using this tutorial.
Contact Form using PHP
Let’s begin by creating a simple HTML file, which will serve as our client side script. In this HTML page, I will just create the text that you need to put in the body tags.
- Create a form in HTML using the form tag and define the method and action as follows.
<formĀ method="post" action="mailer.php"> //FORM CODE HERE </form>
- Now, inside this form, we will need create input fields, which will ask the user for various details and then a submit button at the end. Add this code there.
Name: <input type="text" name="name" value="Bob"/><br \> Email: <input type="text" name="email" value="[email protected]"/><br \> Gender: <input type="radio" name="gender" value="male" /> Male<br \> <input type="radio" name="gender" value="female" /> Female<br \> Comments:<br \> <textarea name="comments" rows=5 cols=60>Any other comments you'd like to add?</textarea><br/> <input type="submit" value="Send Email"/>
- The HTML coding part is complete now. Your HTML file should look something like this.
Now, let us get to creating our PHP script, which will enable us to send the email to our prescribed address.
- Create a new PHP file and start with the starting and ending points of the PHP file.
<?php //PHP Code here ?>
- Now here, we need to get the data from the form, i.e. the data that the user has entered. To get that, we have to use the following code. Each variable will define every different input of the form.
$name = $_POST['name']; $email = $_POST['email']; $gender = $_POST['gender']; $comments = $_POST['comments'];
- Now, we need to create the actual message that will be sent and will also define the Subject and the TO-email address. We create this by joining all the string that we have in a manner we want. Here, I will join them in this way.
$to = '[email protected]'; $subject = 'Contact form Results'; $message = 'Name: '.$name. "\r\n". 'Email: '.$email. "\r\n". 'Gender: '.$gender . "\r\n". 'Message from user: '. $comments;
- In order to make our contact form a lot more professional, it is advised to add a few additional headers to the email that we send. We will add the following headers to our email.
$header = 'From: '. $name . '<' . $email. '>'. "\r\n" . 'Reply-To: '.$email . "\r\n". 'X-Mailer: PHP/'.phpversion();
- Now, we will add the validation checks to make sure that the values entered by the user are correct and not fraud. We do this by adding the following code.
$check1 = strpos($email, '@', 1); if($check1 == true) { $check2 = strpos($email, '.', 4); if($check2 == true) { if(mail($to,$subject,$message,$header)) echo "Email sent successfully!"; else echo "Email Sending Failed!"; }else{ echo "The email address you entered is invalid!"; } }else{ echo "The email address you entered is invalid! Please Check it!"; }
- Now, save the file as “mailer.php”, in the same directory as that of the HTML form. That’s it. This is how your PHP file should look like at the end.
If you wish to download this entire source code, you can do that here.
Download Source Code
Here is a screenshot of the Email that I received using the form we just created.
You can always improvise on the looks of the form by applying some CSS. In the future, I might share some tutorials on how to do that. Till then, stay subscribed to Slash Coding via RSS Feeds, Facebook or Twitter.
JOAO CARLOS FERREIRA BORGES JUNIOR says
Thanks for the tutorial. However, when testing in Notepad ++ with Chrome, when I was sending nothing happened, the php page script appeared. Any suggestion.
Thank you,
Aneesh Bhatnagar says
Hey!
You’ll need to run the php script on a server. If you are developing on your local machine, try setting up Xampp or a similar stack. Hope that helps.
Aneesh