1 of 8

PHP- Reading data from web form controls like text boxes, radio buttons, lists etc.

2 of 8

Introduction

  • We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and $_POST.
  • The form request may be get or post.
  • To retrieve data from get request, we need to use $_GET, for post request $_POST.
  • $_GET is an array of variables passed to the current script via the URL parameters.

  • $_POST is an array of variables passed to the current script via the HTTP POST method.

3 of 8

$_GET

  • GET transfers the information through http head location & displays the data on URL address.
  • GET is unsecured.
  • GET may be used for sending non-sensitive data.
  • GET transfers limited amount of data.
  • GET can't upload the file.
  • Because the variables are displayed in the URL, it is possible to bookmark the page.

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send.

4 of 8

$_POST

  • POST transfers the information through document body.
  • POST is highly secured.

1. POST requests do not remain in the browser history

2. POST requests cannot be bookmarked

  • POST transfers huge amount of data.
  • POST can upload the files.

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

5 of 8

Form.html

<html>

<body>

<form action="welcome.php" method="get">  

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

<input type="submit" value="visit"/>  

</form>  

</body>

</html>

<?php  

$name=$_GET["name"];  

echo "Welcome, $name";  

?>  

welcome.php

Output:

Welcome raj�

6 of 8

Login.html

<form action="login.php" method="post">   

<table>   

<tr>

<td>Name:</td>

<td> <input type="text" name="name"/></td>

</tr>  

<tr>

<td>Password:</td>

<td> <input type="password" name="password"/></td>

</tr>   

<tr>

<td colspan="2"><input type="submit" value="login"/>  </td>

</tr>  

</table>  

</form>   

<?php  

$name=$_POST["name"];  

$password=$_POST["password"];  

 echo "Welcome: $name, your password is: $password";  

?>  

Login.php

7 of 8

8 of 8