get and post method



To read the data from a form, we can use the following three superglobal variables.


array Description
$_GET Contains all the field names and values sent by a form using the get method
$_POST Contains all the field names and values sent by a form using the post method
$_REQUEST Contains the values of both the $_GET and $_POST arrays combined, along with the values of the $_COOKIE superglobal array

Each of these three superglobal arrays contains the field names from the sent form as array keys, with the field values themselves as array values.


<input type="email" name="email" value=""/>  

You could then access the value that the user entered into that form field using either the $_GET or the $_REQUEST superglobal:


	      	$email = $_GET["emailAddress"]; 
			$email = $_REQUEST["emailAddress"];
	      

Different between GET and POST


GET sends its variables in the URL. It easy to see what was sent with GET. And user can change what was sent There is usually a low limit on the number of characters that can be sent in a URL. If we try to send long variables using GET, we may lose some of them.


POST sends its variables behind the scenes and has a much higher limit. It limit is usually several megabytes.


Browsers will not automatically resend post data if your user clicks the Back button, you may get a message like "The data on this page needs to be resent". This does not happen with GET, browsers will just resend data as needed through URL.


PHP has post_max_size entry in php.ini. It is usually set to 8M by default, which is 8 megabytes.


The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.


The GET method is restricted to send upto 1024 characters only.



Get method


Now try below the code is example for code save in testing.php script and run in localhost server


	      	<?php
			   if( $_GET["name"] || $_GET["email"] ) {
			      echo "Welcome ". $_GET['name']. "<br>";
			      echo "You email is  ". $_GET['email']."<br>";
			      
			      exit();
			   }
			?>
			<html>
			   <body>
			   
			      <form action = "" method = "GET">
			         Name: <input type = "text" name = "name" />
			         email: <input type = "email" name = "email" />
			         <input type = "submit" />
			      </form>
			      
			   </body>
			</html>
	      

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.


The POST method does not have any restriction on data size to be sent.


The POST method can be used to send ASCII as well as binary data.


The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.


post method


Now try below the code is example for code save in testing.php script and run in localhost server


	      	<?php
			   if( $_POST["name"] || $_POST["email"] ) {
			      if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
			         die ("invalid name and name should be alpha");
			      }
			      echo "Welcome ". $_POST['name']. "<br>";
			      echo "You email is  ". $_POST['email']."<br>";
			      
			      exit();
			   }
			?>
			<html>
			   <body>
			   
			      <form action = "" method = "POST">
			         Name: <input type = "text" name = "name" />
			         email: <input type = "email" name = "email" />
			         <input type = "submit" />
			      </form>
			   
			   </body>
			</html>
	      


The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies.


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


request method


Now try below the code is example for code save in testing.php script and run in localhost server


	      	<?php
			   if( $_REQUEST["name"] || $_REQUEST["email"] ) {
			      echo "Welcome ". $_REQUEST['name']. "<br />";
			      echo "You email is  ". $_REQUEST['email']."<br>";
			      exit();
			   }
			?>
			<html>
			   <body>
			      
			      <form action = "" method = "POST">
			         Name: <input type = "text" name = "name" />
			         email: <input type = "email" name = "email" />
			         <input type = "submit" />
			      </form>
			      
			   </body>
			</html>
	      




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o