In the previous tutorial, we talked about
how to create a form and then
accessing data submitted by user.
When a form is filled by the user, the data's entered needs to be processed and retrieved.
This tutorial is basically about how to send and retrieve data in PHP. This means that you will learn to store data on the server and retrieve them using the GET and POST variables.
The
$_GET and
$_POST are superglobal variables which means that they can be accessed regardless of the scope.
We talked earlier about the how the POST method and GET method works in the last post which was about
PHP forms when user enter information in a form and how you retrieve them as an admin. But, we didn’t talk deep about it so I taught it would be useful if we go deep and understand the POST and GET method perfectly.
The
POST and GET method are the methods of sending information to the server in PHP. A web browser communicate with server using one of the
HTTP method which are GET and POST method.
The GET method
The
$_GET is an array of variables passed to the current script via
URL parameters.
The
$_GET method can access all information sent through URL or through HTML form. The GET method allows data to be sent as URL parameters that are mainly strings and values separated by ampersand.
All I am saying is
– data sent by the get method are displayed in the URL.
But when exactly do you need to use GET?
As I said earlier, information sent from a form using GET is displayed on the URL which means that it is visible to everyone.
The GET method has a limitation of
2000 characters. But with the use of GET, it is possible to bookmark the page. This means that if you want it possible to
bookmark, then you should consider using GET method.
But, never use get to store SENSITIVE information like password or other sensitive information.
Why?
Because the parameters are displayed and are visible to anyone using the website.
Now, let’s see how to send information via the GET method.
<!DOCTYPE HTML>
<html>
<head>
<title>Example of PHP Get method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
echo "<p>Your name is,".$_GET["name"]. "</p>"
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here is a screenshot
The POST method
Data or information sent from the
POST method is NOT visible. This means that all values are embedded within the body of HTTP request. But the amazing fact is that it has
no limit of information to be stored unlike the GET method which has limitation of 2000 characters.
You know I said that the GET method allows you to bookmark a page, the POST method does not allow this.
But you can store sensitive information on the POST method.
The POST method is more secure than the GET because the information entered by the user is not visible in the URL.
Okay so let’s see how to send information via Post method.
<!DOCTYPE HTML>
<html>
<head>
<title>Example of PHP Get method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
echo "<p>Your name is,".$_POST["name"]. "</p>"
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here is a screenshot
But you need to secure your form. A tutorial will be made on that later.
Follow us on
Please share
Comments
Post a Comment