Making a contact form in Php.
Introduction:
For this you will need basic php knowledge, patience and a willingness to learn. You will also need a text editor ( I am using notepad for example ) and a means of testing out this script, a server on your pc for example.
Part 1 - The form.
Well seeing as we are making a contact form I thought it best to start off with making the form. This is only simple html and I expect you all know this already. But for a contact form I am going to include:
- A name box
- A contact drop down list
- A subject box
- A email box
- A message box
- A submit button
- A reset button
So on with the code:
Code:
<div align="center">
<form action="" method="POST">
<b> Please enter your name:</b><br>
<input type="text" name="name" size = 30><br><br> <!-- the name box //-->
<b> Please choose a contact: </b><br>
<select name="contact">
<option value="1"> Person 1
<option value="2"> Person 2
<option value="3"> Person 3
</select><br><br>
<b> Please enter a subject:</b><br>
<input type="text" name="subject" size = 30><br><br><!-- the subject box //-->
<b> Please enter your email address:</b><br>
<input type="text" name="email" size = 30><br><br><!-- the email box //-->
<b> Please enter your message: </b><br>
<textarea name="message" cols = 20 rows = 10>
</textarea><br><br>
<input type="submit" value="submit" name="submit_1"><br>
<input type="reset" value="reset">
</form>
</div>
Now that we have our form sorted we can move on to the really interesting stuff, the php! Now you'll have to read and pay attention to this to fully understand so pay attention!
Part 2 - Declaring the variables.
Now when the user presses submit on the form, the form values ( the nanme, contact, subject, email and message ) are all "POSTED" to the php script that is usually found in the form's action tag like below:
Code:
<form action="scripthere.php" method="POST">
As you can see in our example we don't have an action, but that's because I'm going to show you a neat little trick using the isset() function! But more of that later. So we want to declare the form values into variables that we can use in our php script.
Code:
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
?>
Great! Doesn't look like much and it sure doesn't do anything! Now we need to stop and think, we are taking in input from people off the internet. Things could go wrong! You could get people who forget to fill in certain parts of the form, or people ( for what ever reason ) who feel the need to submit empty forms and spam your inbox! How can we stop this? Well I'm going to show you the error control.
Part 3 - Error control.
So if the user has submitted a form and any of the fields are left blank that means that the php variable we delcared before for that field will be blank! so we need to check IF the variable equals "". We need to declare a new variable first, and I'll tell you why a little later on!
Code:
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
?>
Now that we have the new variable we can start with our if statements to check if the variables are blank and if they are we add a message to the variable "$error".
Code:
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
?>
So as you can see above if they leave a textbox blank then we add a sentence to a variable called $error asking the user to fill it in again. The if statements dont have the curl brackets "{ }" because the if statement is all on one line. If the if statement went onto two lines I would need the curly brackets. So we have our error control done! Hold up you may be thinking , we haven't checked the $contact variable. Well read on and you will find out.
Part 4 - Sorting out those contacts ( with a security measure ).
Now if you look at the part of the form where the user selects a contact you will see the following:
Code:
<select name="contact">
<option value="1"> Person 1
<option value="2"> Person 2
<option value="3"> Person 3
</select>
This means if the user chooses to contact person 1 then the variable $contact will have the value "1", if they chose person 2 $contact will have a value of "2" and if they decided to contact person 3...you guessed it $contact has the value of "3". Now hold up one minute, this is the person they want to contact, the numbers 1, 2 and 3 aren't email address' how can you email them?! Well that was my security measure. If someone were to look at the source code of the sit and seen this:
Code:
<select name="contact">
<option value="topsecretemail@test.co.uk"> Contact me
</select>
They would have your email address and then that would render the contact form pointless! So we are going to use a lovely little thing called a switch (case). This is the code:
Code
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
case 2:
$contact = "person2@testmail.com";
break;
case 3:
$contact = "person3@testmail.com";
break;
default:
$contact = "person1@testmail.com";
break;
}
?>
Now there's a lot to explain so listen up! Basically the switch (case) is like a group of if statements. You start off by declaring the switch statement with suprisingly:
Code:
switch
You then delcare the variable you want to check , in our case $contact.
Code:
switch ($contact)
Then you add your cases, as you can see in our example, we have case 1, 2, 3 and default.
Code:
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
What has happened above is as follows. The switch case checks the value of $contact, if it equals "1" it runs the code in "case 1" so in our example it changes the value of $contact to person1@testmail.com, then I use a break statement to break out of the switch statement, otherwise it would run through all of the cases! Then at the end I added a default case, this is used incase the value of $contact doesn't equal either 1, 2 or 3.
Now we have sorted out all our variables , and checked them.
Part 5 - To send the email or not to send the email , that is the question.
Now this is a contact form we only want to send the email IF the whole form has been filled in correctly. If it hasn't we want to send the user a error message and tell them to try again. Now look at our code, if the form has been filled in correctly then the variable $error will still equal nothing :
Code:
$error = "";
HOWEVER if the user has left a field blank then the $error message will contain a sentence, for example:
Code:
$error = "*Please enter a name"."<br>";
So we will only send the email IF $error = "", lets update our code:
Code:
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
case 2:
$contact = "person2@testmail.com";
break;
case 3:
$contact = "person3@testmail.com";
break;
default:
$contact = "person1@testmail.com";
break;
}
if($error==""){
//code here to send the email
}
else{
echo "<div align='center'>". $error ."</div>";
}
?>
There we go now if the user doesn't fill the form in properly, the email isn't sent, and they are told where they made a mistake! Beautiful!
Part 6 - Bringing it together a little bit.
Remember in part 2 when I told you I was going to teach you about the cool little function "isset()". Well my friends this is the time! As well as that we will also be involving our html form into our php! So let's begin.
The user fills the form in and presses the submit button, the variables are then all "posted" to the php script. We can make a variable for the submit button! Now if we have a variable for the submit button we can tell if the user has pressed the submit button or not! If they have pressed it then the variable will be there! If they haven't pressed it then it wont! So let's add the isset() function to our code:
Code:
<?php
if(isset($_POST['submit_1'])) {
[/color]
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
case 2:
$contact = "person2@testmail.com";
break;
case 3:
$contact = "person3@testmail.com";
break;
default:
$contact = "person1@testmail.com";
break;
}
if($error==""){
//code here to send the email
}
else{
echo "<div align='center'>". $error ."</div>";
}
}
else {
?>
<div align="center">
<form action="" method="POST">
<b> Please enter your name:</b><br>
<input type="text" name="name" size = 30><br><br> <!-- the name box //-->
<b> Please choose a contact: </b><br>
<select name="contact">
<option value="1"> Person 1
<option value="2"> Person 2
<option value="3"> Person 3
</select><br><br>
<b> Please enter a subject:</b><br>
<input type="text" name="subject" size = 30><br><br><!-- the subject box //-->
<b> Please enter your email address:</b><br>
<input type="text" name="email" size = 30><br><br><!-- the email box //-->
<b> Please enter your message: </b><br>
<textarea name="message" cols = 20 rows = 10>
</textarea><br><br>
<input type="submit" value="submit" name="submit_1"><br>
<input type="reset" value="reset">
</form>
</div>
<?php
}
?>
Now you maybe looking at that and thinking , what the . . . but don't be scared! It's quite simple. We check to see if the user has pressed submit:
Code:
if(isset($_POST['submit_1'])) {
if they have we move onto our php script, if they haven't we display the form! Genius!
Part 7 - The code to send the email!
Now the code above may look a little tricky but don't worry, if you've read the tutorial, then you should understand it as I've broken it down piece by piece. Now we get onto the stuff that the contact form was made for, sending the email! And for this we are going to use the mail() function.
Firstly I'm going to describe the mail() function a little bit. We use our pre-defined already checked variables as parameters is the mail() function.
Here is how it is:
Code:
mail($contact, $subject, $message, $headers);
Now we need to add a new variable, $headers, this will include basic information about who the email is from. Let's update our code:
Code:
<?php
if(isset($_POST['submit_1'])) {
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
$headers = 'From:'.' '. $email;
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
case 2:
$contact = "person2@testmail.com";
break;
case 3:
$contact = "person3@testmail.com";
break;
default:
$contact = "person1@testmail.com";
break;
}
if($error==""){
mail($contact, $subject, $message, $headers);
}
else{
echo "<div align='center'>". $error ."</div>";
}
}
else {
?>
<div align="center">
<form action="" method="POST">
<b> Please enter your name:</b><br>
<input type="text" name="name" size = 30><br><br> <!-- the name box //-->
<b> Please choose a contact: </b><br>
<select name="contact">
<option value="1"> Person 1
<option value="2"> Person 2
<option value="3"> Person 3
</select><br><br>
<b> Please enter a subject:</b><br>
<input type="text" name="subject" size = 30><br><br><!-- the subject box //-->
<b> Please enter your email address:</b><br>
<input type="text" name="email" size = 30><br><br><!-- the email box //-->
<b> Please enter your message: </b><br>
<textarea name="message" cols = 20 rows = 10>
</textarea><br><br>
<input type="submit" value="submit" name="submit_1"><br>
<input type="reset" value="reset">
</form>
</div>
<?php
}
?>
There we go! Our script now takes input from the user via a form, checks the data, if the data is good then it sends the email. But how will we know if the email was sent sucessfully or failed to send?
Part 8 - Success?
We can do this with a simple if statement around the mail function:
Code:
if(mail($contact, $subject, $message, $headers)){
echo " Email was sent ";
}
else {
echo " Email failed to send ";
}
There we go, let's add this to our script:
Code:
<?php
if(isset($_POST['submit_1'])) {
$name = $_POST['name'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$error = "";
$headers = 'From:'.' '. $email;
if($name=="") $error.="*Please enter a name"."<br>";
if($subject=="") $error.="*Please enter a subject"."<br>";
if($email=="") $error.="*Please enter a email"."<br>";
if($message=="") $error.="*Please enter a message"."<br>";
switch ($contact){
case 1:
$contact = "person1@testmail.com";
break;
case 2:
$contact = "person2@testmail.com";
break;
case 3:
$contact = "person3@testmail.com";
break;
default:
$contact = "person1@testmail.com";
break;
}
if($error==""){
if(mail($contact, $subject, $message, $headers)){
echo " Email was sent ";
}
else {
echo " Email failed to send ";
}
}
else{
echo "<div align='center'>". $error ."</div>";
}
}
else {
?>
<div align="center">
<form action="" method="POST">
<b> Please enter your name:</b><br>
<input type="text" name="name" size = 30><br><br> <!-- the name box //-->
<b> Please choose a contact: </b><br>
<select name="contact">
<option value="1"> Person 1
<option value="2"> Person 2
<option value="3"> Person 3
</select><br><br>
<b> Please enter a subject:</b><br>
<input type="text" name="subject" size = 30><br><br><!-- the subject box //-->
<b> Please enter your email address:</b><br>
<input type="text" name="email" size = 30><br><br><!-- the email box //-->
<b> Please enter your message: </b><br>
<textarea name="message" cols = 20 rows = 10>
</textarea><br><br>
<input type="submit" value="submit" name="submit_1"><br>
<input type="reset" value="reset">
</form>
</div>
<?php
}
?>
No comments:
Post a Comment