3

Hi i am looking at differences between $_GET and $_POST methods , i came across some articles that says

  • A POST request also has $_GET parameters

  • So a POST request is a superset of a GET request; you can use $_GET in a POST request, and it may even make sense to have parameters with the same name in $_POST and $_GET that mean different things.

When should I use GET or POST method? What's the difference between them?

http://www.sitepoint.com/on-get-and-post/

And there is an example also

For example, let's say you have a form for editing an article. The article-id may be in the query string (and, so, available through $_GET['id']), but let's say that you want to change the article-id. The new id may then be present in the request body ($_POST['id']).

so how can we use $_GET['id'] and $_POST['id'] at the same time , confusing . Please explain this with a simple example . so everyone including me , who does not understand this can understand well .

Thank you in advance :)

Community
  • 1
  • 1
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

3 Answers3

4

I'll explain to you by using an example:

<form method='post' action='edit-article.php?article_id=3'>
    <label for='article_name'>Article name:</label>
    <input type='text' name='article_name' value='' />
    <input type='submit' name='edit' value='Change article name' />
</form>

When you press submit you will be redirected to edit-article.php?article_id=3

Here you will have the following variables set: $_GET['article_id'] (from url), $_POST['article_name'](from form) and $_POST['edit'] (the submit button, also via form)

Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45
besciualex
  • 1,872
  • 1
  • 15
  • 20
3

Perhaps the simplest way to understand this is that $_GET is simply badly named. All it actually represents is the values of "query string" parameters parsed from the part of a URL after a ?. Since every request has a URL, whatever type it is, any request can populate $_GET.

$_POST, on the other hand, is populated only for POST requests, and even then only those whose request body is in a particular format.

When you use method=get in HTML, the browser just creates a URL based on the form data, and asks for that URL with a GET request the same as you typing it into the address bar. With method=post, the form data is sent separately from the URL, but the URL might still contain a ? and a query string.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
1

Think of it like this. You have two completely different arrays:

$A = array();
$B = array();

Now you can write this piece of code:

$A['id'] = 8;
$B['id'] = 5;

The above code is completely valid. These are different arrays, they just happen to have the same keys with different values assigned to them.

$_GET and $_POST are different variables. Everything you write into the url query will show up in the $_GET variable, evrerything you send via POST will end up in $_POST. So you can set the same key in the URL query and in the POST data.

However, $_REQUEST holds the data of $_GET, $_POST and $_COOKIE. If you have the same keys in $_POST and $_GET we can assume, that $_REQUEST will hold only one of the values. I actually do not know, which value will be saved in $_REQUEST and I hope someone else knows the answer to that, because I am very curious about that.

Ziga Petek
  • 3,953
  • 5
  • 33
  • 43