Thursday 10 April 2014

Creating a Simple Contact Form In php

Contact us form sending email in php
Creating a simple contact Us Form in Php. As you had seen most of website creates the contact form for visitors to contact them so this tutorials we are creating the contact form with sending an email in php. We will Create a Contact Form and will Send Email to Our self that contact form.

Code For Creating a Simple Contact Form In Php


<?php



if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])) {



$contact_name = $_POST['contact_name'];

$contact_email = $_POST['contact_email'];

$contact_text = $_POST['contact_text'];



if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {



if(strlen($contact_name)>25 || strlen($contact_email)>50 || strlen($contact_text)>1000) {

echo 'sorry, an error occurred. Please try again later.';

}else{

$to = 'roxsmohammed@gmail.com';

$subject = 'Contact Form Submitted.';

$body = $contact_name. "\n".$contact_text;

$headers = 'From: '.$contact_email;



if(@mail($to, $subject, $body, $headers)) {

echo 'Thanks for Contacting Us. We\'ll be in touch soon.';

}else {

echo 'Sorry, an error occurred. Please try again later.';

}

}

}else {

echo 'All Fields are required.';

}

}

?>



<form action="email.php" method="POST">

Name:<br><input type="text" name="contact_name" maxlength="25"><br><br>

Email Addres:<br><input type="text"name="contact_mail" maxlength="50"><br><br>

Message:<br>

<textarea name="contact_text" rows="6" cols="30" maxlength="1000"></textarea><br><br>

<input type="submit" value="send">

</form>


Output (Screenshot1)

Creating Simple Contact Form In Php
Screenshot1
This Tutorials Deals With Submitting the contact Form. So, Say for Example You Seen website which have Contact Us Page and You filling You Information and you click to the submit button. You can Retrieve data users Filled Form By storing in database but for now we will sending an email address sending a message to specific email address and that how we are going to be do we will fill the contact form from user and will mail to specific email address.

Code Html Contact Form  

<form action="email.php" method="POST">
Name:<br><input type="text" name="contact_name" maxlength="25"><br><br>
Email Addres:<br><input type="text"name="contact_mail" maxlength="50"><br><br>
Message:<br>
<textarea name="contact_text" rows="6" cols="30" maxlength="1000"></textarea><br><br>
<input type="submit" value="send">
</form>

Firstly we Create a contact Form in html form action is going to be email.php so we are submitting back to same page and this is going to be submitted POST data we are going to submit large amount of data that we don't want to shown in url bar so we use the POST method to send the data from the form. In Form first field i am going to create is name and i am going to break out after this i am going to say input type equal to text because we are taking text value for the name and the name of this is contact_name and then we will break down. Secondly email Address and we going to break and we going to say input type equal to text again name equal to contact_email then break and i am not going to add phone number fields and other its upto you if you want you can add into your contact form. Thirdly message break we going to put textarea so, textarea is just a large textarea to write text. I will give name of contact_text we also give them rows and colons. Breaks down Submit Button as soon as the form is filled and submit button is clicked the email will be send to specific email address. You can also provide designs to the contact form by providing css to html tags. But its simple Contact Form in Php.

Php Script to send the email of the contact Form Filled By Visitors To Specific Email address. When the Form is submitted First Thing we had to do check whether form is submitted and each elements is set. So, we use isset construct and we way isset $_POST because remember we were submitting this variables using POST method variable. So, We pick them up By $_POST and inside square brakcets we need to specify variable to pass in and this is contact_name and then we need to say (&&)and  we apply and operators if contact_name is set and isset in $_POST in square brackets we need to specify next field that we are submitting contact_email and similarly contact_text we will check the value of the html form is set


if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])) {

Warning: We are Not validating Contact Form the reason for this is its to long Code and unnessary i will be writing another article of email validation
$contact_name = $_POST['contact_name'];
$contact_email = $_POST['contact_email'];
$contact_text = $_POST['contact_text'];


Next thing we had to do is we set them to there variables. So, we can use them lot easier inside if statement. So, I will create Individual variable for each one say $contact_name equal to $_POST['contact_name'];. You were be thinking why we are doing set variables inside if statement outside if statement we get error of undefined index error. Declaration of the variable is always be done after they set. In this way we get out three variable $contact_name , $contact_email and $contact_text. 

if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {

Above Piece of code will check whether form field are empty if they will be empty it will give error message All Fields are required. We go to else part of if statement.


if(strlen($contact_name)>25 || strlen($contact_email)>50 || strlen($contact_text)>1000) {

Above piece of code will check the whether the name is not more then 25 character. email address user is providing must not be more then 50 character. and the text message by user must not be more then 1000 character. otherwise it will not send message and execute else part to the visitors 'sorry, an error occurred. Please try again later.

If This all If statement is Satisfied then email php script will execute. 

$to = 'roxsmohammed@gmail.com';
$subject = 'Contact Form Submitted.';
$body = $contact_name. "\n".$contact_text;
$headers = 'From: '.$contact_email;

if(@mail($to, $subject, $body, $headers)) {
echo 'Thanks for Contacting Us. We\'ll be in touch soon.';
}

Above Piece of code will mail the name, email and message to specific email address here it will email me to my email address roxsmohammed@gmail.com. Read More On Sending email In php. This was the Tutorial for Simple Contact Form and email it to specific email address.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates