GET and POST methods and how to get info from them

There are two ways we may get info from users by using a form: GET and POST methods. Additionally, GET method may be used for other purposes as a regular link. Let´s check both methods

Post method
Get method
An example: three versions

POST method

This method will be indicated in the form we are using to get information from user as shown in the example bellow
 

<form method="POST" action="GetandPost.php"> 
Your name<BR>
<input type=text name=thename size=15><BR>
Your age<BR>
<input type=text name=theage size=15><BR>
<input type=submit  value="Send info">
</form> 
Your name

Your age


Does not work

When submitting the form we will visit the URL bellow (will be different when using GET method):

http://www.phptutorial.info/learn/GetandPost.php
When getting information from the form in the response page we will use $_POST command
 
 
Code Output
<?php print $_POST["thename"]; ?> John
<?php print $_POST["theage"]; ?> 30
<?php
$thename=$_POST["thename"];
$theage=$_POST["theage"];

print "Hi ".$thename.", I know you are ".$theage." years old";
?>
Hi John, I know you are 30 years old

 

GET method

This method may be used exactly as in the example above, but the URL we will visit after submission will be diferent.

In the example bellow we have removed  the word "POST" and "GET" has been written instead.
 

<form method="GET" action="GetandPost.php"> 
Your name<BR>
<input type=text name=thename size=15><BR>
Your age<BR>
<input type=text name=theage size=15><BR>
<input type=submit  value="Send info">
</form> 
Your name

Your age


Does not work

When submitting the form we will visit the URL bellow:

http://www.phptutorial. info/learn/GetandPost.php?thename=John&theage=30
When getting information from the form in the response page we will use $_GETcommand.
 
 
Code Output
<? print $QUERY_STRING; ?> thename=John&theage=30
<?php print $_GET["thename"]; ?> John
<?php print $_GET["theage"]; ?> 30
<?php
$thename=$_GET["thename"];
$theage=$_GET["thename"];

print "Hi ".$thename.", I know you are ".$theage." years old";
?>
Hi John, I know you are 30 years old


An example: three versions

Get method may be used for additonal porposes. Although Post method is not used in this example, a similar page may be create. In the example bellow it is shown data in different ways depending on $QUERY_STRING  or $_GET values obtained from the the url visited.
 

Getandpostexample.php
<html>
<body bgcolor=FFFFFF>
<pre>
<b>Information about my friends</b>

<?php if ($QUERY_STRING=="showall") { ?>
Anna
 From London. Student
Paolo
 From Roma. Student
Andoni
 From Donosti. Student

<a href=Getandpostexample.php>Hide data</a>
<?php }else{ ?>
<?php  if ($_GET["name"]=="Anna") { ?>
Anna
  From London. Student
<?php }else{ ?>
<a href=Getandpostexample.php?name=Anna>Anna</a>
<?php } ?>
<?php  if ($_GET["name"]=="Paolo") { ?>
Paolo
  From Roma. Student
<?php }else{ ?>
<a href=Getandpostexample.php?name=Paolo>Paolo</a>
<?php } ?>
<?php  if ($_GET["name"]=="Andoni") { ?>
Andoni
  From Euskadi. Student
<?php }else{ ?>
<a href=Getandpostexample.php?name=Andoni>Andoni</a>
<?php } ?>
<p>
<a href=Getandpostexample.php?showall>Show all data</a>

<?php } ?>
</pre>

</body>
</html>

The previous code and this one will produce the same result. Just compare them: instead of using open and close tags each time, print command is used. Line breaks ("\n") are included within the text to be printed.

Getandpostexample2.php
<?php
// this code is always shown
print "<html>\n<body bgcolor=FFFFFF>\n";
print "<pre>\n<b>Information about my friends</b>\n";
 
if  ($QUERY_STRING=="showall") {
    print "\nAnna\n From London. Student";
    print "\nPaolo\n From Roma. Student";
    print "\nAndoni\n From Euskadi. Student";
    print "\n<a href=Getandpostexample.php>Hide data</a>";
}else{

    if ($_GET["name"]=="Anna") {
          print "\nAnna\n From London. Student";
    }else{
          print "\n<a href=Getandpostexample.php?name=Anna>Anna</a>";
    }

    if ($_GET["name"]=="Paolo") {
          print "\nPaolo\n From Roma. Student";
    }else{
          print "\n<a href=Getandpostexample.php?name=Paolo>Paolo</a>";
    }

    if ($_GET["name"]=="Andoni") {
          print "\nAndoni\n From Euskadi. Student";
    }else{
          print "\n<a href=Getandpostexample.php?name=Andoni>Andoni</a>";
    }

    print "\n<a href=Getandpostexample.php?showall>Show all data</a>";

}

?>
</pre>
</body>
</html>

This third version uses three variables named $Anna, $Paolo and $Andoni, which are defined in first tree lines of the code. Using variables may be a very interesting option in cases like this one.

Getandpostexample3.php
<?php
// Variables are defined for each person
$Anna = "\nAnna\n From London. Student";
$Paolo = "\nPaolo\n From Roma. Student";
$Andoni = "\nAndoni\n From Euskadi. Student";

// this code is always shown
print "<html>\n<body bgcolor=FFFFFF>\n";
print "<pre>\n<b>Information about my friends</b>\n";
 
if  ($QUERY_STRING=="showall") {
    print $Anna.$Paolo.$Andoni;
    print "\n<a href=Getandpostexample.php>Hide data</a>";
}else{

    if ($_GET["name"]=="Anna") {
          print $Anna;
    }else{
          print "\n<a href=Getandpostexample.php?name=Anna>Anna</a>";
    }

    if ($_GET["name"]=="Paolo") {
           print $Paolo;
    }else{
          print "\n<a href=Getandpostexample.php?name=Paolo>Paolo</a>";
    }

    if ($_GET["name"]=="Andoni") {
          print $Andoni;
    }else{
          print "\n<a href=Getandpostexample.php?name=Andoni>Andoni</a>";
    }

    print "\n<a href=Getandpostexample.php?showall>Show all data</a>";

}
?>
</pre>
</body>
</html>

PhpTutorial.info