Passing Form Fields
by Joel Sutton <[email protected]>
In the last column we discussed some of PHP's basic concepts. This
issue we will extend this knowledge by creating two scripts. One
which generates a form which can accept user input, and the second
will receive that input and process it.
Unlike normal procedural programming (where a given program runs
from start to finish), web based applications are what we call
state-less. This roughly means that they only run for a short
period of time (the time it takes for the web server to send the
page to you). This makes things a little tricky because we have to
end the program before we accept any input.
So, as the programmer, it's our job to set things up so that the
programs that we write can remember where they're up to. There are
usually three ways to achieve this:
- session variables
- passing form fields
- URL encoded fields
In this article we're going to look at form fields by running
through a simple example.
Suppose that we would like to add a visitor feedback form to our
site. When a user comes along to our site they will be able to
choose a link which takes them to the feedback form.
<html>
<head>
<title>Feedback form</title>
</head>
<body>
<form action="feedback_commit.php3" method="POST">
<h1>Feedback Form</h1>
<pre>
Name: <input type="text" name="full_name">
Email: <input type="text" name="email">
Comment:
<textarea name="comment" cols="60" rows="5">
</textarea>
</pre>
<center>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>
If you are familiar with HTML you will recognise this as a stock
standard form. Notice how the form tag has an action option, and it
points to a php3 file. It is this tag that links the user input to
the PHP program. Once the user hits the submit button, all of the
form information is sent to the php script.
<?
/* Email Feedback system
* by Joel Sutton
* FreeBSDzine - May Issue
*
* Version: 1.0
*
* Last Modified: Sat May 13 16:35:39 EST 2000
*
* Description:
* This script takes the output of feedback.html and emails certain
* variables to the email address specified in the mail function
* call.
*
*/
// Use sprintf to format a nice text message
//
$msg = sprintf("
Feed back form results
=========================================================
Name: %s
Email: %s
Comment:
%s
", $full_name, $email, $comment) ;
// Send the message
//
mail("[email protected]", "Feed Back Form", $msg) ;
/***********************************************************************/
?>
<html>
<head>
<title>Feedback Results</title>
</head>
<body>
<h3>Request processed</h3>
<p>Thank you <? print $full_name ?> for your feedback.</p>
</body>
</html>
If you observe carefully, you'll notice that the names of some of
the variables (which are the words preceded with a dollar sign)
match the names of the fields in our form.
This script simply copies the information out of those variables,
makes some formatting changes and sends it off in an email (using
the mail command).
Next month we will extend this simple program by adding some error
checking code, to ensure that the visitor provides some relevant
information.
|