Professional Web Development Forums: Make a contact form with PHP

Jump to content

Make a contact form with PHP Bookmark

User is offline ElseAndrew 

  • English & Proud -__-
  • Group: Moderator
  • Posts: 1,919
  • Joined: 12-February 09
  • Gender:Male

Find Posts Tutorial info

  • Added on: 15 May 2009 - 11:00 PM
  • Date Updated: 30 March 2010 - 02:49 AM
  • Views: 800
Contact form!





So, you want to make a contact form for your website? Okay cool you have come to the right place.


Before I continue I would like to state this tutorial is copyrighted to Prowebforums.com ONLY reposting is not allowed.


Right first of all you will need a form, this may be a form you already have or one that you will be making, for the purpose of this I

will make a form with you now.


Make a new document and name it contact.html





1.


Ok so start every new HTML page by opening a new HTML tag.


<html> 

 <head>

  <title>Contact us!</title>

  </head>

  

</html>
Okay so that's done. Everything you wish to be displayed on your page goes into the <body> tag so lets continue by opening them!


<html>

 <head>

  <title>Contact us!</title>

  </head>

  

  <body>

  

  </body>

 </html>
Next we need to acctually make our form!



2.



 <html>

 <head>

  <title>Contact us!</title>

  </head>

  

  <body>

  

 <html>

 <head>

  <title>Contact us!</title>

  </head>

  

  <body>

  

    <form method="post" action="contact.php"> 


    Name: 


        <input type="text" name="firstname"> 


    Email: 


            <input type="text" name="email"> 


    Reason: 


                <input type="text" name="reason" id="reason"> 


                    <input type="submit" value="Submit" name="submit"> 


    </form>

  

  </body>

 </html>


  

  

Im guessing if your looking at this you already know how to use HTML, but I shall explain it anyway.



<form method="post" action="contact.php">



Ok, so first of all you call your form by using the <form> tag and then you use alittle thing call "post" you may be thinking whats this for?


You shall find this out later on when we start our PHP.


action="contact.php" simply gets the location of your PHP which shall be created next.



Okay, save this document and thats the form done.



But your thinking "My reason box is a little small!" This can be fixed by addign some CSS which is why we gave it the id of reason.



All CSS belongs in the <head> tag and should be started with <style type="text/css"> but <style> would work fine.



3.



 <style type="text/css">

 

    //For calling and id you use the #

    

        #reason { //open the start of your code.

            width: 250px; // This will make the width of our code 250 pixels, but you may use what ever width fixs your needs.

        } // close our tag.

</style>
Okay so that's done, you can go ahead and copy this into your <head></head> tags in the conact.html page.


Our updated code should now look like this:


     

 <html>

 <head>

  <title>Contact us!</title>

  

  <style type="text/css">

  

    #reason {

        width: 250px;

        height: 150px;

    }

    

 </style>

  

  </head>

  

  <body>

  

    <form method="post" action="contact.php"> 


    Name: 


        <input type="text" name="firstname"> 


    Email: 


            <input type="text" name="email"> 


    Reason: 


                <input type="text" name="reason" id="reason"> 


                    <input type="submit" value="Submit" name="submit"> 


    </form>

  </body>

 </html>
4.



Okay, so where ready to start out PHP, make a new document and name it contact.php.



PHP starts pretty simple really by using



<?php



and



?> will close your PHP.



You first need to create your varibles for your PHP to talk to the contact.html.



5. Creating The varibles:



 <?php

 

    $firstname = $_POST['firstname'];

    $email = $_POST['email'];

    $reason = $_POST['reason'];

    $space = " ";

    

?>
Ok thats that.


What this does is get the data from the form using $_POST which I said I will tell you about before. method="post" which is why you need to declare it


Varibles in PHP must start with a dollar sign, then we name them something which can relate to in the furture.


Then you use $_POST['firstname'];


Your varible must always and with a semi-colan ';'


You may be thinking wtf wheres this firstname come from?? Well its also from yours form if you recall we use name="firstname".


A full break down of this would be


$_POST : method="post"

['firstname'] : name="firstname"


That was kinda hard to explain but I hope you get it.


6. How do I send the mail to my inbox?!


That comes next! Slow down, you need to first create a couple more varibles!


$to = "you@example.com";

$subject = "You have mail from:" .$space . $email;



Ok, edit this code to your email address or you wont get mail! Then the subject in plain text will say:


You have mail from: you@example.com


To add more than one varible per statement is done with a '.' so as you see we close our text string after the : then add a .$varname . $varname .


And so on...


An updated version of our code so far:



 <?php

 

    $firstname = $_POST['firstname'];

    $email = $_POST['email'];

    $reason = $_POST['reason'];

    $space = " ";

    

    $to = "you@example.com";

    $subject = "You have mail from:" .$space . $email;

    

?>
7. Creating the message area!


Now to create the message! For this your need to make another varible, yes another! :D


$msg = "



From: $firstname n

Email: $email nn

Reason: $reason

";



This is pretty simple really with what you have already read you should understand! There is a new thing we have used with is n

this is just a new line! As you cant use HTML n will start a new line for you.


Updated code:


 <?php

 

    $firstname = $_POST['firstname'];

    $email = $_POST['email'];

    $reason = $_POST['reason'];

    $space = " ";

    

    $to = "you@example.com";

    $subject = "You have mail from:" .$space . $email;

    

        $msg = "

    

        From: $firstname n

        Email: $email nn

        Reason: $reason 

    ";

    

?>
8. Let's make this mail send!


PHP has a very handy function called mail() that will send the mail directly to your inbox and its used like this:


mail($to, $subject, $msg);


You might be thinking what the hell...


I will break it down for you


First of all you open your mail function


mail (


Then you have to add your varibles in there or you will be receiving nothing!


mail($to


This adds now will go to your email address next:


mail($to, $subject


Now the subject works! notice the comma? When using mail() you must separate your varibles using the , or it wont work! :) Next:


mail($to, $subject, $msg);


No comma is used for the end varible as your not using another. So you close your tag and end with a semi-colon. That's the mail all set up!


Updated version of our code:


 <?php

 

    $firstname = $_POST['firstname'];

    $email = $_POST['email'];

    $reason = $_POST['reason'];

    $space = " ";

    

    $to = "you@example.com";

    $subject = "You have mail from:" .$space . $email;

    

        $msg = "

    

        From: $firstname n

        Email: $email nn

        Reason: $reason 

    ";

    

        mail($to, $subject, $msg);

    

?>
That's it done. This will now work just fine, but you can read on to see how to validate the email feild.


9. VALIDATE THE EMAIL FEILD!


function check_email($email) {

$validation = TRUE;

if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) {

$validation = FALSE;

}

return $validation;

}



if(!check_email($mail)) {



echo "Thanks for the mail!";



} else {

echo "Invalid...";

}





Okay so your thinking WTF?!


It's okay I will break it down line by line.


function check_email($email) { // This creates the function check_mail and checks it agaist the email varible we made before! And



$validation = TRUE; // Checks if its true!


if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) { This line use Reg Exp to check all feilds of the email, im not going

explain it all now as it could go on for awhile and its out of the depth of this tutorial. Copy and paste ;) One thing I will explain is

we use the !ergi this simply means NOT



$validation = FALSE; // Then we return false

} // and closes our if{} statement!

return $validation; // Returns the varible.

} // Closes the function.



if(check_email($email)) { // This is pretty much the same as above it checks the function check_email against $email if its true



echo "Thanks for the mail!"; // It returns true and we use echo to print out the text on the web browser.



} else { // This is used to tell the person they have entered an invalid email.

echo "Invalid...";

}



A full version of the code is now below. Well done on creating your first Contact Page! Upload it to your web server and enjoy!



[CODE]

<?php



$firstname
= $_POST['firstname'];

$email = $_POST['email'];

$reason = $_POST['reason'];

$space = " ";



$to = "";

$subject = "You have mail from:" .$space . $email;



$msg = "



From: $firstname n

Email: $email nn

Reason: $reason

"
;



mail($to, $subject, $msg);



function
check_email($email) {

$validation = TRUE;

if(!
eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) {

$validation = FALSE;

}

return
$validation;

}



if(
check_email($email)) {

echo
"Thanks for the mail!";

}

elseif(!empty(
$firstname))

{

echo
"Thanks for the mail!";

}

elseif(!empty(
$reason))

{

echo
"Thanks for the mail!";

}

else {

echo
"Invalid...";

}



?>



Now tested and works 100%, thanks to chrisrules for alittle fix.
0

Share:

Powered by (IM) Tutorials 1.1.0 © 2010, by Michael McCune

Enter your sign in name and password


Sign in options
  Or sign in with these services