422,929 Members | 1,355 Online
Bytes IT Community

Parsing Forms With PHP

By Blair Ireland
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.

The Form

First off though, we must have a form to work with. We will use the following form as the example during this tutorial...

<HTML>
<HEAD>
<TITLE>Form Handling with PHP</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM METHOD=POST ACTION="tellus.php3">
<TABLE>
<TR height="20">
<TD colspan="2"><FONT SIZE="-1" face="verdana">Below is a Sample Form for our PHP tutorial </TD>
</TR>
<TR height="50">
<td></td>
</TR>
<TR>
<TD align="left"><FONT SIZE="-1" face="verdana">Your Name Your E-Mail Address</td>
<td><INPUT TYPE="text" NAME="name">
<br>
<INPUT TYPE="text" NAME="email">
</TD>
</TR>
<tr>
<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>
</SELECT>
<br>
<INPUT TYPE="submit" value="Tell us!">
</td>
</tr>
</TABLE>
</FORM>
</BODY>
</HTML>

Just copy and paste that HTML to a file, lets call it, well, form.html

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...

The Code

Ok, it's time to tell you how PHP handles form inputs. The file to handle the code will be called tellus.php3 .

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 "form.";
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.";
PRINT "</FONT></TD></TR></TABLE></center>";

?>

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.

  Send E-Mail Script »