Hey, I’ve finally got around to making you a cool PHP Tutorial, with a practical use. If you’re a web designer, this will allow you to add contact forms to your clients websites, and if you wanna get into php, this is a nice little tutorial that will get you going.
What you’ll need:
Linux server, php enabled (version 4 or 5 is fine), basic html knowledge
What you’ll achieve in this tutoiral:
A simple form which will be able to email information from a form, learn basic php.
Introduction
PHP can do many things, I’d even say it will go as far as your imagination (or someone else’s imagination), will take it. Some people belive it is even restricted to small limited scripts, but this is not so. The last time I checked popular web-based programming languages, PHP was the MOST popular programming language online. So why not join the revolution of open source programming, join PHP! (Hypertext Preprocessor).
Ok lets start…
Step 1
Lets design a nice useable form, when I say useable, I mean, just a working one, you can tart it up yourself.
Create yourself a .html page, called test.html - That’ll do nicely. I also suggest you work from a folder.
In your test.html, put this code in, as you’re already hopefuly familiar with html, this should be ok for you and not need explaining, if not, I suggest viewing this through a wysiwyg editior (what you see is what you get - like dreamweaver)
Here’s the code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“> <html xmlns=”http://www.w3.org/1999/xhtml“> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ /> <title>Email Comment Form Test</title></head>
<body> <form id=”comment-form” name=”comment-form” method=”post” action=”sendmail.php”> <table width=”500″ border=”0″ cellspacing=”0″ cellpadding=”3″><tr> <td align=”right”>Name</td><td><label> <input name=”name” type=”text” id=”name” /></label>
</td></tr><tr> <td align=”right”>Email</td> <td><input name=”email” type=”text” id=”email” /></td> </tr><tr><td align=”right”>Telephone</td> <td><input name=”telephone” type=”text” id=”telephone” /></td> </tr><tr><td align=”right” valign=”top”>Comment</td> <td> <textarea name=”comment” cols=”30″ rows=”4″ id=”comment”></textarea> </td></tr><tr><td> </td><td> <label><input type=”submit” name=”Submit” value=”Submit” /> </label>
</td></tr></table></form></body> </html>
Step 2 - Preparing the PHP sendmail.php
Ok just to explain the code above, you should now have a form which ask’s for the users name, email, telephone number & comments in a larger free text field.
You should also notice that top form tag surround the form points to sendmail.php - This basically tells the form to look for sendmail.php to process it.
So here’s the php you need to make that form work:
<?php
// send email script
$gname = $_POST['name'];
$gphone = $_POST['phone'];
$gemail = $_POST['email'];
$gcomment = $_POST['comment'];
$to = youremail@hotmail.com;
$subject = “Your Subject of your contact form”;
$body = “Name: $gnamen Email: $gemailn Phone: $gphonen Comments: $gcomment”;
$headers = “From: test@test.co.uk\r\n“;
if (mail($to, $subject, $body, $headers)) {
header(’Location: http://www.yourwebsite.co.uk/your-thank-you-page.html’);
} else {
echo (”<p>Message delivery failed…</p>”);
}
?>
Ok there is a fair amount going on there, so lets explain. The first bit is actually just a comment, stating who the script is made by. You can adapt this to make it your own comment, its important to identify your php documents for future reference.
Next up is the $gname = $_post['name']; and this repeats for each field. This is assigning the fields from the form to variables. I just so happend to start my variables with g, you can call them what you like, but make sure its meaning ful.
I then go on to define other parts of the form, including who its to, who its from, subject and the headers.
Headers are important, this can stop your mail from reaching some recievers. I’ve just included one header, and i’ve tested this script and I know it works with HOTMAIL, but not other providers, you will need to include more headers for other providers - please research this yourself.
Next up is the function built into php which sends your mail, it basically collects you variables up, and uses them to send out your mail, it has a if statement, to check if this was successful.
Now you got a thank you page you’ll need to create, just to let the user know this all went ok.
But this should work fine, I’d try sending it to your hotmail, it probably will go into your junk filter, but it will work.
Thanks for reading
Wuup
Very handy tutorial, I’ll give it a shot on a site I’m developing and I’ll let you know the reults! Cheers!