Looks Like you are not Registered with our Site or Not Logged in. Click Here to Register

It shows that you are unregistered. Please register with us by clicking Here


Go Back   DesiPad.Com :: Simply the Best > IT Cafe > IT Discussion > Tutorials
Connect with Facebook

Notices

Tutorials Step by step instructions with screen shots on how to perform different computing tasks.

Help Contribute to DesiPad.Com
Your Donation Will Be Used To Pay For our ever increasing bandwidth costs, our hosting Service, domain registration, software licensing fees, and maintenance costs
We have received a total of $0 in donations towards our goal of $200
0% of our goal has been reached
Reply  Post New Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2006
Admin's Avatar
Ali - Owner
Points: 78,348, Level: 40 Points: 78,348, Level: 40 Points: 78,348, Level: 40
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Jan 2006
Location: United States
Gender: Male
Posts: 2,058
DesiPad-Buck: 5,343.00
Rep Power: 10
Admin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond reputeAdmin has a reputation beyond repute
PHP Advanced contact Form Tutorial

Description: In this tutorial we'll learn how to make an advanced contact form.
Skill: Intermediate
Time needed: 20 minutes

If you followed our simple contact form tutorial, but aren't satisfied with the result, you can expand it using this tutorial. In this tutorla we will explain how to make an advanced contact form with error checking. First we'll post the code and then explain it bit by bit.

So, let's move on to the code:


Code:
 <?

 if($_POST[send]) { 
   if($_POST['name'] != "NULL" AND $_POST['email'] != "NULL" AND $_POST['website'] != "NULL" AND $_POST['message'] != "NULL") {
     if(preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $_POST['email'])) {

$name = $_POST["name"];
$email = $_POST["email"];
$website = $_POST["website"];
$message = $_POST["message"];

$forminfo =
"Name: $name\n
E-Mail Address: $email\n
Website URL: $website\n
Message: $message\n";

$recipient = "YOURADDRESS@YOURDOMAIN.COM";
$subject = "Contact form from website";

$formsend = mail("$recipient", "$subject", "$forminfo");

  echo("
<div align=\"center\">

     Your message has been sent successfully! Thank you.

</div>

  ");

 }else{

  echo("

   <div align=\"center\">
   You have specified an invalid e-mail address. Please hit the button below to check your e-mail address.<br>
   <br>
   <FORM>
   <INPUT TYPE=\"button\" VALUE=\"Back\" onClick=\"history.back()\">
   </FORM>
   </div>

  ");

  }
 }else{
  
  echo("

   <div align=\"center\">
   You have forgotten to fill out one or more fields. Please hit the button below to check your data.<br>
   <br>
   <FORM>
   <INPUT TYPE=\"button\" VALUE=\"Back\" onClick=\"history.back()\">
   </FORM>
   </div>

  ");

  }
 }else{

echo ("

<div align=\"center\">
<form method=\"POST\">
Your Name:<br>
<input type=\"text\" size=\"45\" name=\"name\"><br>
Your E-Mail Address:<br>
<input type=\"text\" size=\"45\" name=\"email\"><br>
Your Website URL:<br>
<input type=\"text\" size=\"7\" name=\"website\"><br>
Your Message:<br>
<textarea name=\"message\" rows=\"7\" cols=\"50\"></textarea><br>
<input name=\"send\" type=\"submit\" value=\"Send\"></td>
</form>

");

}

?>
So, that's a lot of code. Let's start by explaining how it works. First, it checks if all the fields were filled out, and it checks if a valid e-mail address was filled out. If one of the fields was left empty, it returns an error. If an invalid e-mail address was entered, it also returns an error message.
Code:
   if($_POST[send]) { 
   if($_POST['name'] != "NULL" AND $_POST['email'] != "NULL" AND $_POST['website'] != "NULL" AND $_POST['message'] != "NULL") {
     if(preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $_POST['email'])) {

As you can see the first line checks if the user has clicked on the 'Send' button. If they haven't, the form will display the form. But that comes later in this tutorial. If the user has clicked on the send button, the form will check if the following fields were left blank:

Name
E-Mail Address
Website
Message


In this case it checks for all fields, but you can add, remove or edit it so it checks only the fields you want it to check. If that's ok, and nothing was left blank, it checks if a valid e-mail address was entered. It actually checks the format of the e-mail address, not if it excists.

Next stop in the code, are the fields that are submitted:

Code:
 $name = $_POST["name"];
$email = $_POST["email"];
$website = $_POST["website"];
$message = $_POST["message"];

The above means that the data filled out in the form will be submitted to the e-mail address specified in the code. It doesn't actually e-mail it, but it defines the values entered.

Now we move on to the fields that are actually e-mailed to us:


Code:
 $forminfo =
"Name: $name\n
E-Mail Address: $email\n
Website URL: $website\n
Message: $message\n";
The form as it is displayed above will be displayed in our e-mail address inbox as a message. $name, $email, etc is defined in the first part of this code, as explained above.

Below we'll define our recipient and the subject of the e-mail, so you know it's a message sent from your website:
Code:
  $recipient = "YOURADDRESS@YOURDOMAIN.COM";
$subject = "Contact form from website";

$recipient will be your e-mail address, since you are the one who wants to receive all messages.
$subject is the subject of the e-mail message.

The next part is the message that will be displayed on the user's screen once they've submitted the message and all fields were filled out correctly:


Code:
   echo("
<div align=\"center\">


     Your message has been sent successfully! Thank you.

</div>

  ");
You can edit this in any way you like, it's just an echo that's displayed on the screen.
Now, when the user has submitted an invalid e-mail address the following message will be displayed:

Code:
 }else{

  echo("

   <div align=\"center\">
   You have specified an invalid e-mail address. Please hit the button below to check your e-mail address.<br>
   <br>
   <FORM>
   <INPUT TYPE=\"button\" VALUE=\"Back\" onClick=\"history.back()\">
   </FORM>
   </div>

  ");
Also this can be edited in every way you'd like to edit it, as long as you don't edit the }else{ tags. Everything in between the echo fields is the message that will be displayed on the screen, so that's the things you can edit.

Now for the last error check, the checking for empty fields:


Code:
   }
 }else{
  
  echo("

   <div align=\"center\">
   You have forgotten to fill out one or more fields. Please hit the button below to check your data.<br>
   <br>
   <FORM>
   <INPUT TYPE=\"button\" VALUE=\"Back\" onClick=\"history.back()\">
   </FORM>
   </div>

  ");
The above message is the same as the other message, but now it outputs that the user has forgotten to fill out one or more fields.

And now for the last part of this code, the form itself:

Code:
   }
 }else{

echo ("

<div align=\"center\">
<form method=\"POST\">
Your Name:<br>
<input type=\"text\" size=\"45\" name=\"name\"><br>
Your E-Mail Address:<br>
<input type=\"text\" size=\"45\" name=\"email\"><br>
Your Website URL:<br>
<input type=\"text\" size=\"7\" name=\"website\"><br>
Your Message:<br>
<textarea name=\"message\" rows=\"7\" cols=\"50\"></textarea><br>
<input name=\"send\" type=\"submit\" value=\"Send\"></td>
</form>

");

}

As you can see the names of the forms are related to the names of first part of this code. You should keep these names related since the script won't work if you don't. An example:

Quote:
<input name=\"send\" type=\"submit\" value=\"Send\"></td>
Is related to:

Code:
  if($_POST[send]) {
If you change the name of the input form, then the code above doesn't know the user clicked the send button and thus it doesn't execute the code.

So, that's everything you need to do, and know, to create an advanced contact form. We hope you've learned something from this tutorial.
The Following User Says Thank You to Admin For This Useful Post:
ksyz_1 (06-15-2007)
  #2 (permalink)  
Old 02-09-2006
@shfaq @hmad's Avatar
Junior
Points: 4,340, Level: 6 Points: 4,340, Level: 6 Points: 4,340, Level: 6
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Gender: Undisclosed
Posts: 180
DesiPad-Buck: 25.00
Rep Power: 5
@shfaq @hmad will become famous soon enough@shfaq @hmad will become famous soon enough
Wow, Great yar, Thanks for sharing.
  #3 (permalink)  
Old 02-23-2006
minhajsh's Avatar
Junior
Points: 2,193, Level: 4 Points: 2,193, Level: 4 Points: 2,193, Level: 4
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Age: 28
Posts: 101
DesiPad-Buck: 28.00
Rep Power: 4
minhajsh is on a distinguished road
Re: PHP Advanced contact Form Tutorial

thanks for thsi
  #4 (permalink)  
Old 02-23-2006
minhajsh's Avatar
Junior
Points: 2,193, Level: 4 Points: 2,193, Level: 4 Points: 2,193, Level: 4
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Age: 28
Posts: 101
DesiPad-Buck: 28.00
Rep Power: 4
minhajsh is on a distinguished road
Re: PHP Advanced contact Form Tutorial

thank u very much
  #5 (permalink)  
Old 02-23-2006
tekas's Avatar
Freshman
Points: 2,029, Level: 3 Points: 2,029, Level: 3 Points: 2,029, Level: 3
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Posts: 30
DesiPad-Buck: 25.00
Rep Power: 4
tekas is on a distinguished road
Re: PHP Advanced contact Form Tutorial

Quote:
Originally Posted by minhajsh
thank u very much
Thanks a lot
  #6 (permalink)  
Old 02-26-2006
hiddendragon's Avatar
Freshman
Points: 2,060, Level: 3 Points: 2,060, Level: 3 Points: 2,060, Level: 3
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Posts: 21
DesiPad-Buck: 25.00
Rep Power: 0
hiddendragon is on a distinguished road
Re: PHP Advanced contact Form Tutorial

cool, thanks for sharing
  #7 (permalink)  
Old 02-27-2006
rehch's Avatar
Freshman
Points: 2,063, Level: 3 Points: 2,063, Level: 3 Points: 2,063, Level: 3
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Posts: 25
DesiPad-Buck: 25.00
Rep Power: 4
rehch is on a distinguished road
Re: PHP Advanced contact Form Tutorial

thanks
  #8 (permalink)  
Old 03-07-2006
DeSi's Avatar
Banned
Points: 1,990, Level: 3 Points: 1,990, Level: 3 Points: 1,990, Level: 3
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Age: 20
Posts: 9
DesiPad-Buck: 25.00
Rep Power: 0
DeSi is on a distinguished road
Re: PHP Advanced contact Form Tutorial

Thanks ... Reps added ...
  #9 (permalink)  
Old 03-26-2006
nets4eva's Avatar
Junior
Points: 2,229, Level: 4 Points: 2,229, Level: 4 Points: 2,229, Level: 4
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2006
Age: 19
Gender: Male
Posts: 124
DesiPad-Buck: 25.00
Rep Power: 4
nets4eva is on a distinguished road
Re: PHP Advanced contact Form Tutorial

sallam all of u
  #10 (permalink)  
Old 04-18-2006
lionman's Avatar
Junior
Points: 2,500, Level: 4 Points: 2,500, Level: 4 Points: 2,500, Level: 4
Activity: 1% Activity: 1% Activity: 1%
 
Join Date: Apr 2006
Gender: Undisclosed
Posts: 95
DesiPad-Buck: 39.00
Rep Power: 4
lionman is on a distinguished road
Re: PHP Advanced contact Form Tutorial

Nice work. Thanks a lot
  #11 (permalink)  
Old 04-19-2006
rockajay's Avatar
Freshman
Points: 2,059, Level: 3 Points: 2,059, Level: 3 Points: 2,059, Level: 3
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2006
Age: 25
Posts: 42
DesiPad-Buck: 25.00
Rep Power: 4
rockajay is on a distinguished road
Lightbulb Re: PHP Advanced contact Form Tutorial

nice
  #12 (permalink)  
Old 04-19-2006
harshakodam's Avatar
Junior
Points: 2,173, Level: 4 Points: 2,173, Level: 4 Points: 2,173, Level: 4
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2006
Posts: 78
DesiPad-Buck: 28.00
Rep Power: 4
harshakodam is on a distinguished road
Re: PHP Advanced contact Form Tutorial

thanks for sharing
  #13 (permalink)  
Old 05-05-2007
yacoob's Avatar
Junior
Points: 2,579, Level: 4 Points: 2,579, Level: 4 Points: 2,579, Level: 4
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2006
Age: 20
Gender: Male
Posts: 171
DesiPad-Buck: 162.00
Rep Power: 4
yacoob is on a distinguished road
Re: PHP Advanced contact Form Tutorial

Cheers mate
  #14 (permalink)  
Old 06-15-2007
ksyz_1's Avatar
Freshman
Points: 1,259, Level: 2 Points: 1,259, Level: 2 Points: 1,259, Level: 2
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Gender: Male
Posts: 43
DesiPad-Buck: 60.00
Rep Power: 3
ksyz_1 is on a distinguished road
Re: PHP Advanced contact Form Tutorial

nice
  #15 (permalink)  
Old 07-12-2008
M mansoor nazir's Avatar
Freshman
 
Join Date: Apr 2008
Age: 28
Gender: Male
Posts: 2
DesiPad-Buck: 0.00
Rep Power: 0
M mansoor nazir is on a distinguished road
Re: PHP Advanced contact Form Tutorial

please help me to learn php
Reply

Tags
advanced, contact, form, php, tutorial

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
ContactManager v0.11 Cyborg Pocket Pc, PALM, PDA & Windows Mobile Applications 0 08-02-2007 04:43 AM


Credit Cards Mortgage Free Advertising Loans Mortgages


Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
eXTReMe Tracker