PHP Form Handling | GET and POST method PHP

PHP Form Handling | GET and POST method  

PHP Form Handling | GET and POST method

PHP Form Handling | GET and POST method

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.

PHP Get Form

Get request is the default form request. The data passed through get request is visible on the URL browser so it is not secured. You can send limited amount of data through get request.

Let's see a simple example to receive data from get request in PHP.

File: form1.html

  1. <form action="welcome.php" method="get">  
  2. Name: <input type="text" name="name"/>  
  3. <input type="submit" value="visit"/>  
  4. </form>  

File: welcome.php

  1. <?php  
  2. $name=$_GET["name"];//receiving name field value in $name variable  
  3. echo "Welcome, $name";  
  4. ?>  

PHP Post Form

Post request is widely used to submit form that have large amount of data such as file upload, image upload, login form, registration form etc.

The data passed through post request is not visible on the URL browser so it is secured. You can send large amount of data through post request.

Let's see a simple example to receive data from post request in PHP.

File: form1.html

  1. <form action="login.php" method="post">   
  2. <table>   
  3. <tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>  
  4. <tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   
  5. <tr><td colspan="2"><input type="submit" value="login"/>  </td></tr>  
  6. </table>  
  7. </form>   

File: login.php

  1. <?php  
  2. $name=$_POST["name"];//receiving name field value in $name variable  
  3. $password=$_POST["password"];//receiving password field value in $password variable  
  4.   
  5. echo "Welcome: $name, your password is: $password";  
  6. ?>  

Output:

php form php login form handling

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries