If you are new to web development or PHP one thing that may be giving you a headache is form handling. What is form handling? From the name we can gather that it has to do with the processing of user-submitted data... But how? In this tutorial I will teach you how to process a form submitted on your website. Since you are browsing the PHP section of this website, I will discuss processing a form utilizing PHP.
The requirements to complete this tutorial are to have an Apache webserver with at least PHP4 installed. In order to find out if you fulfill these requirements, start by creating a blank file using your favorite editor (wordpad, textpad, notepad, dreamweaver, etc.) and name it "info.php". Inside this file, type the following:
<?PHP
phpinfo();
?>
After you have created "info.php", upload it to your webserver and then point your browser to its location. Once complete you should have a pretty page that lists information about your webserver. If the page did not display anything then your server is certainly not compatible with this tutorial and I suggest finding a PHP enabled host they are easy to come by.
If all is well with your server then it is time for some PHP fun. When using PHP to process a form you have a wide range of options available to you when it comes to what exactly to do with this data. A few of these options are:
1.) Store the data in a database for later retrieval
2.) Sending it to a third party such as a credit card processing gateway to be handled
3.) Having PHP e-mail the data to a specific e-mail address you set
In this tutorial I am covering the third option. It is more common to use a mySQL database to store the information however that is a whole other article by itself which I will cover in my next tutorial. Here is the scenario: You are a webmaster and you need a way for your guests/members to email you a message without exposing your email address to any spiders that were engineered to farm email addresses on the internet and then blast them with spam. The first step in doing this is to create your blank file and then create a form for your users to complete.
<form action='process_form.php' method='POST'>
<fieldset>
<legend>Contact Us</legend>
<label for='email'>Your Email: </label><input type='text' name='email'><br/>
<label for='subject'>Subject: </label><input type='text' name='subject'><br/>
<label for='message'>Your Message: </label><textarea name='message' rows="2" cols="20">
</textarea>
<input type='submit' name='submit' value='Send'>
</fieldset>
</form>
Time to break this little bit down for you..
<form action='process_form.php' method='POST'>
This initiates a form to start. The action is where it sends the browser and data so that it can be processed. In this case we are having our form submit the data to a file on your server called "process_form.php". Another thing to make sure to pay attention to is our method in this case using the POST method. The reason this is important you will see when I get into handling this data. There are two main methods and that is the POST method and GET method, the difference between these two is that if you use POST to send your information it is stored as variables and is invisble to the user. If you use the GET method this information is passed along as a string which gets appended onto the URL and is in turn visible to the user. For submitting data for processing such as passwords and other sensitive information would make the GET method not a good choice unless you encrypt the data visible. There are also other ways of submitting this data however they are beyond the scope of this tutorial.
<label for='email'>Your Email: </label><input type='text' name='email'><br/>
The important thing to look at in this block is where you have a name being applied to your field. When submitting data via a form it is key to name and remember that name for all fields you are taking information from. In this case I have named the Email form field simply "email". A good rule of thumb is to keep your naming as basic and descriptive as possible.
Now that you have your form created you will need to create a file to process the information. Create a new blank file and name it "process_form.php". Inside that form you will need to type the following:
<?PHP
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$target_address = 'yourEmail@RandomDomain.com';
$headers = "From: ".$email."\r\n". "X-Mailer: php";
Remember when we named our form fields "email", "subject", "message" and your variable $target_address needs to be the email address you wish to send the message to. Our variable called $headers tells the script what sort of information needs to be passed along in the email header information. Here we set the from address as the user's email address so that you can easily just press reply to it when you receive the message. Also, a tip to remember is if you are ever interested in sending emails containing HTML you will need to do extra things in your header information. Our next step is to use PHP to actually generate and send the email to the address you specified.
if (mail($target_address, $subject, $message, $headers)) {
echo("Thank you for contacting us, we will get back to you as soon as we can kthxbye");
}
else{
echo("Sorry your message delivery failed. Please try back later");
die();
}
?>
This is very simple code that calls PHP's built in mailing function. First we need to start a conditional statement and inside the parenthesis call your mail function and pass along all the variables you created. The reason we do this is so that the conditional if statement can tell us if the message was sent or not. If it is sent it will display a confirmation message. If the message failed to send it will
display a failure message and then call the die() function to effectively kill the script. When writing PHP make sure you are surrounding your code with both the opening and closing PHP commands e.g <?PHP and ?>. And that would be your final set, you can do many more things with form processing and I will go more deeply into the subject in my next few articles. Please comment if you have any questions or didn't follow me at any point and I will do my best to keep an eye on it.
Good luck -- Domenic DeMatteis
Agamidsoftware.com