What is the meaning of “a POST request also has $_GET parameters”

旧城冷巷雨未停 提交于 2019-12-01 00:44:32

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.

besciualex

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)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!