In this tutorial, I will show you a simple example of using POST or GET in a form on a page to simply print “Hello, NAMEHERE”. Of course, POST and GET can be used for better things than this, like sending form information, but this will be enough so that I can show you the difference between them.
So, firstly, we have our form
Notice in the form tag how we have set method to “post”, this means that the form will submit all values with the post method (so in this case, it will post a variable called namevar). In PHP we can retrieve variables from the post method by using $_POST[‘variablename’]. Since we are using a page that is posting to itself, we will test to see weather the variable exists before we echo anything.Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<form method="post" name="posttest">Name: <input
name="namevar">
<input name="Submit" value="Submit" type="submit">
</form>
</body>
</html>
Take notice of how when the page refreshes, the URL doesn’t change.Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<?php if ($_POST['namevar'] != "")
{
echo 'Hello, ', $_POST['namevar'];
}?>
<form method="post" name="posttest">Name: <input
name="namevar">
<input name="Submit" value="Submit" type="submit">
</form>
</body>
</html>
Now we will use the GET method. First we need to change our form to use the GET method:
In PHP, we can get GET variables by using $_GET[‘variablename’] and again, before printing this variable out, we will test to see if its there.Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<form method="get" name="gettest">Name: <input
name="namevar">
<input name="Submit" value="Submit" type="submit">
</form>
</body>
</html>
However, notice this time that it when the page refreshes it has appended the variable onto the end of the url in the manner of ?namevar=NAMEHERECode:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<?php if ($_GET['namevar'] != "")
{
echo 'Hello, ', $_GET['namevar'];
}?>
<form method="get" name="gettest">Name: <input
name="namevar">
<input name="Submit" value="Submit" type="submit">
</form>
</body>
</html>
GET variables are restricted in length by URL length restrictions. Generally you would use GET to, you guessed it, GET information. Take a forum for example, most forums will use GET variables to show you a topic. URLs with GET variables can be bookmarked, so this is a good idea. However, when you post a message, it uses post variables. For more information on GET vs POST, you can google it, but be careful, sometimes the explanations you find can be confusing.
If this tutorial has been helpful, don’t forget to +rep me!
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
Not bad, you forgot to mention $_REQUEST. +rep
Good one +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks