IT Community - Software Programming, Web Development and Technical Support

Little PhP script

This is a discussion on Little PhP script within the PHP Programming forums, part of the Web Development category; I always get many questions regarding contact forms in PHP in many forums. It is actually very easy to do ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-08-2007, 10:58 PM
spid4r spid4r is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 56
spid4r is on a distinguished road
Default Little PhP script

I always get many questions regarding contact forms in PHP in many forums.

It is actually very easy to do and i will hope to keep it very basic and junk jargon free.

1) So your customer has filled in the form and they really want to contact you. I find using php to handle the form is the fastest way. I get it to handle it in 2 different ways.
1.1) Email me and the customer the details
1.2) Add to a customer database table, no harm in keeping records. and im good at losing emails :x

We will work through this working on pricipal you know how to set up a form.. and using the customers Name, Email, and Question.

1) we need to grab the form details when they click send.

To do this we use the lovely $_POST command.

Right it looks nasty, but works very simply by grabbing the details and sending them to a variable you declare.

so lets see the start of our contactme.php file

Code:
<?PHP
$name = $_POST["name"];
$email = $_POST["email"];
$msg = $_POST["msg"];
?>
Come back... dont be running away just yet... this is the easiest part :shock:

Right lets break one of the lines down.

$name --- Our variable that we are now declaring we want php to hold the data in.

$_POST["name"] --- we are asking php to grab the contents of the form field thats called name.

THATS IT !!! nothing really nasty....Now lets move along to the next section....

So all our variables are loaded with the lovely information we want from the form, now we need to do something with it.

In this tutorial i will only do the very basic checking. We will check the field they are sending isnt empty...

2) Checking the fields :twisted:

Now there are a few ways of doing this. But i will use the most easiest way possible so you can get the idea of what im doing and trying to tell you.

Code:
if (!$name) {  
echo "Please fill in the name field : "; 
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
}
ok GET BACK HERE!!! or wake up, which ever your doing right now.

2.1) IF statement. IM PRAYING TO THE STELLA BEER GOD YOU KNOW THIS ONE !!!

2.2) (!$name) All this command is asking php to do is check that the variable isnt empty. If it is empty, then UTOH... we dont want that so lets move to the next section

2.3) { all this is doing is telling php that we are asking it to run set commands if $name was empty.

2.4) echo" some waffle in here as to what went wrong " The echo command will display on the screen anything you put inbetween the quotes ""

2.5) Now you know the <font> html command so we can ignore that. In this example i closed the php by using ?> and entering my html and then opening php <? again to continue.

2.6) The error has happened our lazy customer didnt enter their name..tut tut tut.. so lets punish them for it. <a href = "javascript:history.back()">Go back</a> Punish them with a blank page just asking them to go back and fill in the name field. And it has a link for them to click.

2.7) DIE !!!!!!! sounds nasty but its only asking PHP to stop doing anything further, as there wasnt want we wanted.

2.8) Do this for every variable you have... so you will get a nice little script forming now.

Our code now looks like this.

Code:
<?

$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg']; 

if (!$name) { 
echo "Please fill in the name field : ";
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

if (!$email) {  
echo "Please fill in the email field : "; 
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

if (!$msg) { 
echo "Please fill in the message field : "; 
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

?>
WOW !!!! its looking FUNKY !!!!! but really its not show us what the said its just keeping all the info there in its variables.

Lets stop for wee wee break and a brew !!! (sending son off to make the tea so place your orders)

Now your back all refreshed, which is good.

so lets move along to emailing the details to you..

3) Emailing form details to you...

mail("welovewebmasterground@webmasterground.com, $email", "$email\n$message", "From: Love You <welovewebmasterground@webmasterground.com>");

SEE ITS SIMPLE !!!

Do i really need to explain this command ?

Ok we will do it briefly.. as i saw sweat on you then...

3.1) Mail is the easiest way to send emails from PHP. Doesnt require any other commands or params to make it move. If you want detailed info on it, check out the php manual.

3.2) "webmasterground@webmasterground.com, $email" --- well we need to send it to our mailbox so we insert our email address. We put $email in there some the customer gets a copy as well. Put whatever emails in there you need to.

3.3) "$email\n$message" --- This displays their email address and message in the email body. the \n command is like <br> in html. Gives you clean line!!

3.4) "From: Love You <welovewebmasterground@webmasterground.com>" --- tells people and us where the email came from, this can be set to anything...

Now lets see our final script !!! WOOOHOOO
Code:
<?

$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg']; 

if (!$name) { 
echo "Please fill in the name field : ";
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

if (!$email) {  
echo "Please fill in the email field : "; 
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

if (!$msg) { 
echo "Please fill in the message field : "; 
?><font face="Tahoma"><a href = "javascript:history.back()">Go back</a><?
die;
} 

mail("welovewebmasterground@webmasterground.com, $email", "$email\n$message", "From: Love You <welovewebmasterground@webmasterground.com>"); 

// This is away to enter php comments into your script.
// The section below just displays a page telling them the email has been sent, then counts down before redirecting them to a page you choose.
?>
<form name="redirect">
<center>
<font face="Arial">
<b>Message Sent to us. We will be in contact shortly. You will be redirected in<br>
<br>
<form><input type="text" size="3" name="redirect2">
</form>seconds</b>
</font>
</center>
<script><!--
var targetURL="http://www.webmasterground.com"
var countdownfrom=1

var currentsecond=document.redirect.redirect2.value=countdownfrom+1
function countredirect(){
if (currentsecond!=1){
currentsecond-=1
document.redirect.redirect2.value=currentsecond
}
else{
window.location=targetURL
return
}
setTimeout("countredirect()",1000)
}

countredirect()
//-->
</script>
Thats it... My little learned friends...

Coming soon (only if this one helps you)

Verifying Emails
Confirming Fields (make them type it twice)
Striping nasty things
and the best MYSQL (YAY)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


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

vB 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
Can an html and Java script be separated and the script placed in the cgi-bin? Pvinothkumar HTML, CSS and Javascript Coding Techniques 1 09-13-2007 06:11 AM
ph ip view script desiinder PHP Programming 1 09-11-2007 03:14 AM
Web Vuser script Shanthi Testing Tools 1 08-29-2007 04:38 AM
Actions and Script sivakumar Web Design Help 1 07-18-2007 01:07 AM
What is your best PHP Directory Script? moinuddin102 PHP Programming 1 04-24-2007 01:03 AM


All times are GMT -7. The time now is 05:09 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0