Parsing Forms With PHP
Senior Editor, TheScripts.com
Parsing Forms With PHP and Sending E-Mail
PHP is excellent for parsing and handling forms. It easily handles this task, making PHP ideal for sending web-based email, outputting information to the screen, and passing data to and from a database.
Parsing forms using PHP is one of the easiest things you could possibly do.... pretty much anyone (and I stress the word anyone) can do this.
First off though, we must have a form to work with. We will use the following form as the example during this tutorial...
<TITLE>Form Handling with PHP</TITLE>
<FORM METHOD=POST ACTION="tellus.php3">
<TD colspan="2"><FONT SIZE="-1" face="verdana">Below is a Sample Form for our PHP tutorial </TD>
<TD align="left"><FONT SIZE="-1" face="verdana">Your Name Your E-Mail Address</td>
<td><INPUT TYPE="text" NAME="name">
<INPUT TYPE="text" NAME="email">
<td colspan="2"><SELECT NAME="opinion">
<option value="is great">I like your site</option>
<option value="is OK">Your Site is OK</option>
<option value="is horrible">Your Site is horrible</option>
<INPUT TYPE="submit" value="Tell us!">
Just copy and paste that HTML to a file, lets call it, well,
You will see in the form action tag that it is pointed at
tellus.php3. This is the PHP 3.0 script that handles all of the form processing commands...
Ok, it's time to tell you how PHP handles form inputs. The file to handle the code will be called
If a form input is named "title", in the PHP script, it will now be known as
$title. There is no complicated form parsing procedures required, this is all automatically done.
Here is an example of what our
tellus.php3 script will look like, which I will explain in detail below;
PRINT "<center><FONT SIZE=-1 face=verdana>"; PRINT "Greetings $name, thank you for filling out our";
PRINT "</center><p><center><TABLE border=0 width=500";
PRINT "cellspacing=0 cellpadding=7>"; PRINT "<TR><TD><FONT SIZE=-1 face=verdana>"; PRINT "In the form, you stated that our site";
PRINT "$status. Thank you for your input."; PRINT "We will send you more information about our site";
PRINT "in an email to $email.";
PRINT "If you have any more comments, feel free to reply.";
Not really much to explain, but you probably noticed that I did indeed reference all the form input names with their
$ prefix respectively. Also note that within the print statements, there are no quotes, as this would prematurely end the print statement.