pTutorial

New way to learn PHP

PHP 5 forms

Form handling in PHP

The ability to retrieve and process user-provided data have become a useful part of any website, PHP provides this powerful feature of process HTML forms easily. You can use the super global array GET, POST or REQUEST to collect the form data.

Simple Html form


<html>
<body>

<form action="" method="post">
<table width="200">
<tr>
<td>Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll NO:</td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Branch:</td>
<td><input type="text" name="branch"></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="send"></td>
<td><input type="reset" name="reset"></td>
</tr>
</table>
</form>

</body>
</html>

Simple HTML form with three field name, roll number, branch and table is used for formatting.

PHP script for access the data

   
 //data.php   
<?php

if(isset( $_POST['sub']))
{
$a=$_POST["name"];
$b=$_POST["roll"];
$c=$_POST["branch"];

echo "Name of student : ".$a."<br>";
echo "Roll no of student : ".$b."<br>";
echo "Branch of student : ".$c."<br>";	
}


?>    

You can put this code into the HTML form page whenever his extension is ".php" otherwise save this file independently and include it or put this file in the action attribute of the HTML form.

PHP script for accessing the form data with the help of POST superglobal array. It simply stores all three data into three different variable and echo.

Simple Html form


<html>
<body>

<form action="" method="post">
<table width="200">
<tr>
<td>Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll NO:</td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Branch:</td>
<td><input type="text" name="branch"></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="send"></td>
<td><input type="reset" name="reset"></td>
</tr>
</table>
</form>

</body>
</html>

Simple HTML form with three field name, roll number, branch and table is used for formatting.

PHP script for access the data

   
    //processform.php
<?php
if(isset( $_GET['sub']))
{
$a=$_GET["name"];
$b=$_GET["roll"];
$c=$_GET["branch"];

echo "Name of student : ".$a."<br>";
echo "Roll no of student : ".$b."<br>";
echo "Branch of student : ".$c."<br>";	
}


?>    

In this example, only the method has been changed (from POST to GET) and all the other things are same.

HTTP Request methods

You can use one of the two methods to submit the form information. These methods pass the form data differently and have different advantages and disadvantages.

$_GET

The form data is passed by adding it to the URL that calls the form processing script. For example, the URL may look like this:-

processform.php?lname=Smith&fname=john

The advantages of this method are simplicity and speed.

The disadvantages are that fewer data can be passed and the information is displayed in the browser, which can be a security problem in some situations.

$_POST

The form data is passed as a package in a separate communication with the processing script.

The advantages of this method are unlimited information passing and security of the data.

The disadvantages are the additional overhead and slower speed.

REQUEST method to process form data

Simple Html form


<html>
<body>

<form action="" method="post">
<table width="200">
<tr>
<td>Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll NO:</td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Branch:</td>
<td><input type="text" name="branch"></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="send"></td>
<td><input type="reset" name="reset"></td>
</tr>
</table>
</form>

</body>
</html>

Simple HTML form with three field, submit, reset button and HTML table is used for formatting.

PHP script for access the data

   
    
<?php
if(isset( $_REQUEST['sub']))
{
$a=$_REQUEST["name"];
$b=$_REQUEST["roll"];
$c=$_REQUEST["branch"];

echo "Name of student : ".$a."<br>";
echo "Roll no of student : ".$b."<br>";
echo "Branch of student : ".$c."<br>";	
}


?>    

In this example, only the method has been changed and all the other things will be same. $_REQUEST superglobal array is used to receive data from GET and POST method.

Note: Forms do not keep any security validation it just shows how to retrieve data from the form.


QUOTES OF THE DAY

Knowledge itself is a power.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER