Previous | Home | Next |
Get & Post Methods
In forms, you want to submit the information and transfered the information to another page. The following to methods are used for this:
- The GET Method : In this method, the key values are passed in the Url.
- The POST Method : In this method, the information transfers in a hidden manner.
In Client side, method="get" is used to submit information in a form. And In Server side, $_GET associative array is used to receive sent information in a form.
Example:
test.php code: <html> <head> <title>Get method in PHP</title> </head> <body> <form action="abc.php" method="GET"> Your Name : <input type="text" name="yourname"/><br/><br/> Your Age : <input type="text" name="yourage" /><br/><br/> <input type="submit" /> </form> </body> </html>
abc.php code: <?php if( $_GET["yourname"] || $_GET["yourage"] ) { echo "Welcome". $_GET['yourname']. "<br />"; echo "You age is ". $_GET['yourage']. " years."; exit(); } ?>
Output:
Before Submit Information:

After Submit Information:

In Client side, method="post" is used to submit information in a form. And In Server side, $_POST associative array is used to receive sent information in a form.
Example:
test.php code: <html> <head> <title>Post method in PHP</title> </head> <body> <form action="abc.php" method="POST"> Name: <input type="text" name="name"/><br/><br/> Age: <input type="text" name="age"/><br/><br/> <input type="submit" /> </form> </body> </html>
abc.php code: <?php if( $_POST["name"] || $_POST["age"] ) { echo "Welcome ". $_POST['name']. "to our world...!!!<br />"; echo "You are ". $_POST['age']. " years old.<br/>"; echo "You permitted to enter this world."; exit(); } ?>
Output:
Before Submit Information:

After Submit Information:

The $_REQUEST variable is a superglobal Array that contains the contents of both $_GET, $_POST, and $_COOKIE arrays. It can be used to collect data sent with both the GET and POST methods.
Example:
test.php code: <html> <head><title>$_REQUEST Variable in php</title> </head> <body> <form action="abc.php?thename=MarPlo" method="post"> <input type="text" name="thename" value="r4r.co.in" /> <input type="submit" value="Send" /> </form> </body> </html>
abc.php code: <?php if(isset($_REQUEST['thename'])) echo $_REQUEST['thename']; ?>
Output:
Before Submit:

After Submit:

Previous | Home | Next |