Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Getting Started : A simple tutorial : Dealing with Forms

Dealing with Forms

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here is an example HTML form:

Example 2.6. A simple HTML form

<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called. In this file you would write something like this:

Example 2.7. Printing data from our form

Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

A sample output of this script may be:

Hi Joe. You are 22 years old.

Apart from the htmlspecialchars() and (int) parts, it should be obvious what this does. htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page. For the age field, since we know it is a number, we can just convert it to an integer which will automatically get rid of any stray characters. You can also have PHP do this for you automatically by using the filter extension. The $_POST['name'] and $_POST['age'] variables are automatically set for you by PHP. Earlier we used the $_SERVER superglobal; above we just introduced the $_POST superglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET superglobal instead. You may also use the $_REQUEST superglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data. Also see the import_request_variables() function.

You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them. We also have a short introduction to handling data received from XForms in our features section.

Code Examples / Notes » tutorial.forms


[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"]
Be careful, when using and processing forms which contains
<input type="image">
tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server.
Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them.


In the sample above, the code dose not work. Even if you copy and paste the sample code you will get:
Hi . You are 0 years old
The code should look more like this on the main page:
<?php echo '

Hello World'; ?>
<form action="action.php" methhod="get">

Your name: <input type="text" name="name" />

your age: <input type="text" name="age" />

<input type="submit" />
Then the action page should look like this:
Hi <?php echo htmlspecialchars($_GET['name']); ?>.
You are <?php echo (int)$_GET['age']; ?> years old


Here is a PHP script to add more form fields in a page by clicking on a button on the same page.You can add unlimited number of fields dynamically(in run time) in a page by using this script.
(For the alligning of form fields,forms are given in layers)
<style type="text/css">
#Layer1 {
#Layer2 {
/*For example this script allows user to add more and more file upload fields in a page by clicking on a button*/
<!--creating a form--><!--save this page as "upload.php" -->
<div id="Layer1">
 <form name="form1" method="post" action="upload.php">
<!--creating a hidden field and sets it's value as $t-->
   <input name="hf" type="hidden" id="hf" value="<? echo ++$t; ?>">
<!--creating a button.When we click this, a new field will be generated-->
   <input type="submit" name="Submit" value="one more">
<!--another form to submit the uploaded file to be processed-->
<!--the upload script should be written in the file "uploader.php"-->
<div id="Layer2">
<form action="uploader.php" method="post" enctype="multipart/form-data">

 echo "Filename:"
 <label for="file">
   <input type="file" name="file" id="file" >
 <input type="submit" name="submit" value="Submit" >
Like this, you can add any type of form field into the same page by the click of a button.


As Seth mentions, when a user clicks reload or goes back with the browser button, data sent to the server, may be sent again (after a click on the ok button).
It might be wise, to let the server handle whatever there is to handle, and then redirect (a redirect is not visible in the history and thus not reachable via reload or "back".
It cannot be used in this exact example, but as Seth also mentions, this example should be using GET instead of POST


According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.
You should use the GET method when your form is, well, getting something off the server and not actually changing anything.  For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.

Change Language

Follow Navioo On Twitter
What do I need?
Your first PHP-enabled page
Something Useful
Dealing with Forms
Using old code with new versions of PHP
What's next?
eXTReMe Tracker