Wednesday, March 18, 2009

Accessing data from html forms in php

Whenever a form is submitted to php, the information from the form is automatically available to php. Php then process the information of the form.

There are several ways to access the html form data into php.

1)Using $_GET, $_POST and $_REQUEST variable:
The $_GET variable is used to collect values from a form with method="get" .

When using the $_GET variable all variable names and values are displayed in the URL.

With GET method in form maximum 100 characters can be send.

The $_GET method is useful for bookmark.

The $_POST variable is used to collect values from a form with method="post". In the url the values are not shown, so this method would be used to pass sensitive information to the server.

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

2)Using $HTTP_POST_VARS variable. But it would be used because this variable may be disabled.

3)By setting the PHP directive register_globals to ON. From PHP 4.2.0 and onwards this variable default value is changed from ON to OFF. This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. So don't rely on this feature.

Below is the simple html form. Whenever you click submit button process_form.php will be called.

<html>
<title>Simple Testing of Reading forms</title>
<body>
<form action="process_form.php" method="post">
Your Name: <input type="text" name="name"><br />
Age: <input type="text" name="age"><br />
Class: <select name="Classes">
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select> <br />
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

Here is the process_form.php code.

<html>
<body>
<?php
echo "Your name is" . $_POST["name"] . " and you are". $_POST["age"] ." years old.
You read in class ". $_POST["Classes"];
?>
</body>
</html>

Related Documents
Working with cookie in php(set, retrieve, delete)
Accessing data from html forms in php
Track error messages on php
Array declaration, assignment and printing in php
Variables in PHP
Constants in php
Magic constants in php

No comments:

Post a Comment