Form handling with GET, POST, and REQUEST in PHP

Form handling is one of the common tasks when creating the project in PHP. There are various methods are available for this and they are good according to the situation.
You can either pass the form data by URL and hidden them from the user.

Contents

  1. $_GET
  2. $_POST
  3. $_REQUEST

1. $_GET

  • 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. The limitation is about 2000 characters.
  • However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
  • $_GET may be used for sending non-sensitive data.
Example

<?php
if(isset($_GET['submit'])){

   $name = $_GET['name'];
   $email = $_GET['email'];
   $age = $_GET['age'];

   echo "name : ".$name."<br>";
   echo "email : ".$email."<br>";
   echo "age : ".$age;
}

?>

<form method='get' action=''>

   Name : <input type='text' name='name' /> <br/>
   Email : <input type='text' name='email' /> <br/>
   Age : <input type='text' name='age' /> <br/>
   <input type='submit' value='Submit' name='submit'>

</form>
 

2. $_POST

  • Information sent from a form with the POST method is invisible to other and has no limits on the amount of information to send.
  • Moreover, $_POST support advanced functionality such as support for multipart binary input while uploading files to server.
  • However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Example

<?php
if(isset($_POST['submit'])){

   $name = $_POST['name'];
   $email = $_POST['email'];
   $age = $_POST['age'];

   echo "name : ".$name."<br>";
   echo "email : ".$email."<br>";
   echo "age : ".$age;
}

?>

<form method='post' action=''>

   Name : <input type='text' name='name' /> <br/>
   Email : <input type='text' name='email' /> <br/>
   Age : <input type='text' name='age' /> <br/>
   <input type='submit' value='Submit' name='submit'>

</form>
 

3. $_REQUEST

  • This method is work as GET method if it is use in <form method=” >.
  • It contains the contents of $_GET, $_POST, and $_COOKIE.
Example

<?php
if(isset($_REQUEST['submit'])){

   $name = $_REQUEST['name'];
   $email = $_REQUEST['email'];
   $age = $_REQUEST['age'];

   echo "name : ".$name."<br>";
   echo "email : ".$email."<br>";
   echo "age : ".$age;
}

?>

<form method='request' action=''>

   Name : <input type='text' name='name' /> <br/>
   Email : <input type='text' name='email' /> <br/>
   Age : <input type='text' name='age' /> <br/>
   <input type='submit' value='Submit' name='submit'>

</form>
 

 Conclusion

Use methods according to your requirement within the <form >. With $_REQUEST method you can either read POST or GET method data but avoid it because this should open security holes in your website.
 

 

 


CONVERSATION

0 Comments:

Post a Comment

Back
to top