123

How Make Your Website More Dynamic by Allowing User Input to Impact Your Content

Handle Forms in PHP and Process User Input

B. Rock
Forms allow you to get information from the user. By using a php script to handle that form, you can do all kinds of cool things - log a user in, register a user, add a user to a mailing list, personalize content, etc.

Getting the information from a form to a php script is simple. It's up to you to figure out what to do with it.

Creating the Form

First, let's create a simple form to handle. We'll build it with two fields - name and age. Take a look at the sample html (form.html) in the first picture. In the code below, I've replaced the normal brackets with parentheses, so that it renders correctly.

(form action="process.php" method="post")
(p)First Name:
(input type="text" name="name /)(/p)

(p)Age:
(select name="age")
(option value="10")10(/option)
(option value="20")20(/option)
(option value="30)30(/option)
(/select)(/p)

(input type="submit" value="Submit" /)
(/form)

The sample form contains a few things - a form tag, an input tag, a select tag, and a submit tag.

You can create the form to hold whatever fields you like, but there's three things you want to keep in mind.

The "action" attribute of the form tag determines what page will handle the input. In our case, this is process.php.

The "method" attribute of the form tag determines how the information gets there. By "posting" the information, it goes directly to the script and is hidden from the user.

If you set method to "get," all of the information will be appended to the url in a format like process.php?name='Joe'&age='20'. This has its uses, but for now we'll stick to post.

Finally, the "name" attribute of each field will be needed to access them in the php script.

Reading the Form

Once you hit Submit on the form, all of the information gets sent to process.php.

If we set our form method to "post" all of the information will be delivered in a special array - $_POST. The elements of $_POST are named after the fields in the form - so we'll be looking for $_POST["name"] and $_POST["age"].

For example, if we wanted to fill a variable with the name, we'd use the line...
$userName = $_POST["name"];

If we set our form method to "get," we would access the $_GET variable instead. To repeat the above example...
$username = $_GET["name"];

Check out the second picture to see the beginning of the script process.php. A modified version of the code is included below.

(?php
$userName = $_POST["name"];
$userAge = $_POST["age"];

$msg = "";

if ($userAge == 10)
$msg= "You're just a kid!";
else if ($userAge == 20)
$msg = "You almost done with college?";
else if ($userAge == 30)
$msg = "You don't still live with your mother... do you?";
?)

It's pretty simple. It puts the user's name into one variable, puts the user's age into another variable, and creates a customized message based on the user's age.

When you originally access the form data, you should perform some validation or error checking on it. For example, you might check to see that it isn't blank. If you were posting the information to a website, you might check to see that there were no html tags present.

Creating Some Output

Now that we've handled the form, let's create some output based on the input. We defined a few variables at the beginning of process.php, so we can now insert them into the html section of the page.

Take a look at the third picture to see the sample code. Here's a modified version of the code.

(html)(body)
(p)Hello (?php echo $userName ?)(/p)
(p)(?php echo $msg ?)(/p)
(/body)(/html)

It prints two paragraphs. The first says, "Hello [name]." The second prints our customized message. This is a pretty simple example of how to process forms and create personalized output, but there are dozens of more useful applications.

Some Possible Uses for Forms

It's hard to imagine a good website without forms because they make so many things possible. Here are a few examples of what you can do.

Register a user. In the form, take a bunch of personal information - including a username and e-mail address. In the processing page, open a database connection and input all of the user's information. You could also use the information to send a confirmation e-mail to the user and reprint all of the information entered in the form.

Create a log in script. In the form, take a username and a password as input. Send the information to a script that checks the username and password against data held in a database. If it matches, the user can be logged in. If not, redirect the user to the log in screen again.

Leave comments. Include a form at the bottom of each of your pages that takes a blob of text, an e-mail address, and a name as input. This is sent to a processing form to be added to the database with a reference number for the article. The next time the article is loaded, check the database for any comments.There are dozens of other uses. Start with the basics and play with it for a while.

User input is one of the basic requirements for a good dynamic website. The only way to get it is through processing forms.

Published by B. Rock

I'm a recent graduate, a newly wed, and a (no longer first year) teacher. I teach HS Social Studies in a New Jersey city. I graduated from the Rutgers Grad School of Ed in May of 2007. In July '07, I...  View profile

  • Forms allow you to get user input to your php scripts
  • All of the information from a form is stored in a special array - $_POST or $_GET
  • The "method" attribute of the form determines how the information is sent to your script.

1 Comments

Post a Comment
  • Osman5/25/2012

    asasas

To comment, please sign in to your Yahoo! account, or sign up for a new account.