The FreeBSD 'zine

May 2000 : PHP Central

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:

  1. session variables
  2. passing form fields
  3. 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.

    <title>Feedback form</title>
  <form action="feedback_commit.php3" method="POST">
  <h1>Feedback Form</h1>
  Name:     <input type="text" name="full_name">
  Email:    <input type="text" name="email">
            <textarea name="comment" cols="60" rows="5">
    <input type="submit" value="Submit">

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
    ", $full_name, $email, $comment) ;
  // Send the message
  mail("[email protected]", "Feed Back Form", $msg) ;
    <title>Feedback Results</title>
  <h3>Request processed</h3>
  <p>Thank you <? print $full_name ?> for your feedback.</p>

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.

Current Issue
. Issue #06 : July 2000

Old Issues
. Issue #01 : February 2000
. Issue #02 : March 2000
. Issue #03 : April 2000
. Issue #04 : May 2000
. Issue #05 : June 2000

Quick Links
. Table of Contents
. Mailing Lists
. FreeBSD Events
. User Group Calendar
. Site Statistics
. Old Articles
. Latest News
. Press Releases
. Contribute
. Send us Feedback
. Other Resources
. Submit an Article
. Submit an Event

Today's Fortune
Illinois isn't exactly the land that God forgot -- it's more like the land He's trying to ignore.

FreeBSD 'zine Poll
Are you going to BSDCon?
What the hell is BSDCon?
Results More polls

. VicFUG

. Issue #01 : Download
. Issue #02 : Download
. Issue #03 : Download
. Issue #04 : Download
. Issue #05 : Download
. Issue #06 : Download


Runs on FreeBSD

Add Channel to My Netscape


Contact: <[email protected]>
This site and the tarballs are built every 6 hours.
Copyright � 1998-2000, The FreeBSD 'zine
Code revision: 07/24/2000��All rights reserved.