Categories

[PHP Tutorial 6] GET and POST Methods in PHP

0

In Tutorial 5, we learnt how to create forms using PHP. To make the forms live and to fetch the data filled in the forms, GET & POST methods are used in PHP. This is a very important while we are creating forms as the data filled by the user has to be stored somewhere.

For this purpose, <form> tag is used and in this tag methods are used that tell where and how the data is to be submitted. The data can be submitted in a PHP script or a URL.

There are two kinds of methods in PHP:

1.GET method
2.POST method

The function of both the methods is same but the difference lies between the two in terms of how the form data is submitted, is explained as follows:

1.GET method is used to submit the data in the form of a URL. The data filled by the user appears after a question mark in the browser. The benefit of this method is that the filled data can be edited in the URL address itself rather than going to the form again and editing it there.

2.POST method is used to submit the data directly to the script or URL mentioned. The data in this case does not appear in the browser. This is, thus, a better method because in case the user is entering a username and password, these will not be shown in the browser.

How to use these methods in PHP scripts?

While creating the forms, we can use these methods as:

<form action="your_script.php" method="post">
Your elements
</form>
<form action="your_script.php" method="get">
Your elements
</form>

[Note: <input type>, <text area>, <submit> etc (that we discussed in Tutorial 5), are all called elements and are written under the <form> tags]

Now, to let the user fill in data in the forms, we need to create variables in our PHP script (here, “your_script”). The variables will store the value entered in the forms. These variables can be declared as:

If POST method has been used, the variable will be declared as:

$variable=$_POST[‘variable’];

If GET method has been used, the variable will be declared as:

$variable=$_GET[‘variable’];

Examples:

1. GET Method:

Consider the following form:

<form action="intro.php" method="get">
First Name: <input type="text" name="text1" />
Last Name: <input type="text" name="text2" />
<input type="submit" value="Submit" /></form>

Now, when the user enters information as, for example,
“First Name: John, Last Name: Bob” and clicks Submit, the resulting URL would be:

http://example.com/intro.php?text1=John&text2=Bob

We can further use this data next time the user comes to the website example.com (in this case) as:

Hi <?php echo $_GET["text1"]; ?> <?php echo $_GET["text2"]; ?>!
//which will show as "Hi John Bob".

2. POST Method:

Consider the same form, this time, using the POST method:

<form action="intro.php" method="post">
First Name: <input type="text" name="text1" />
Last Name: <input type="text" name="text2" />
<input type="submit" value="Submit" /></form>

Now, when the user enters information as, for example,
“First Name: John, Last Name: Bob” and clicks Submit, the resulting URL would be:

http://example.com/intro.php

We can further use this data next time the user comes to the website example.com (in this case) as:

Hi <?php echo $_POST["text1"]; ?> <?php echo $_POST["text2"]; ?>!
//which will show as "Hi John Bob".
Now that you know how to use GET and POST methods in PHP, continue with the
next PHP tutorial on how send mails using PHP. In case of any queries, 
please leave your comment below.
Share.

Leave A Reply