Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

$_SERVER["REQUEST_METHOD"] == "GET" vs $_SERVER["REQUEST_METHOD"] == "POST"

3,123 views
Skip to first unread message

Alla

unread,
Jul 30, 2016, 7:47:24 AM7/30/16
to
Hello!

I will be grateful for your help: I am trying to understand the way these two expressions differ from each other.
Please, take a look at what I have learned thus far and how I understand the topic, and, please, correct me or
explain my mistakes.

$_SERVER["REQUEST_METHOD"] == "GET" and $_SERVER["REQUEST_METHOD"] == "POST"

1) $_POST vs $_GET: both are associative arrays; the difference is that $_GET method displays the
query in the url, for example google.com/search?q=something_i_am_looking_for, while $_POST will
not display user's query.

2) Are $_POST and $_GET somehow correlated to $_SERVER["REQUEST_METHOD"] == "GET" and $_SERVER["REQUEST_METHOD"] == "POST"?

3) For example, there is a form that requires a user to submit some information.

As I have understood based on the way a particular code works (see below), $_SERVER["REQUEST_METHOD"] == "GET" means that a user has clicked on a link, or
was redirected to a page, thus he/she gets to a new page, which should display a certain
form;

but $_SERVER["REQUEST_METHOD"] == "POST" means that the user is already at a
that certain page and sees the form he/she needs to fill in.

Here is an example of the code:


if ($_SERVER["REQUEST_METHOD"] == "GET")
{
// render form
// open a form.php file and display the result on the page
}

// else if the user is already at that form page and sees the form he needs to fill in
else if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// validate submission
if (empty($_POST["username"]))
// do something
etc
}

Thank you very much!

R.Wieser

unread,
Jul 30, 2016, 8:34:31 AM7/30/16
to
Alla,

#1. True (I assume you ment the "GET and PUT *methods*, not the arrays)

#2. Yes, they are. Data that has been send by the GET method will be stored
in the $_GET array, and data that has been send by the POST method will be
stored in the $_POST array.

#3. "$_SERVER["REQUEST_METHOD"] == "GET" means that a user has clicked on a
link," *among other possibilities*, yes.

You see, the GET method is the default method used for all sorts of data
retrieval, ranging from images, sounds, flash content, downloads in general,
etc. Pretty-much the only situation in which a POST is used is when a
form is define *and* that form needs to return (to the server) quite a bit
of data.

> or was redirected to a page

Nope. A POST can be as easily redirected as a GET.

> ... which should display a certain form;

That has got nothing to do with redirection I'm afraid. The redirected-to
page might not be a webpage at all (images, sounds and all other requested
data might be redirected as easily as webpages).

Also, a form (or more of them!) might be present on the page you supplied
the URL to (no redirection involved).

> Here is an example of the code:
...
> // else if the user is already at that form page and sees the form he
needs to fill in

Again, a form *might* be using the POST method, but it doesn't need to.

Regards,
Rudy Wieser


-- Origional message:
Alla <modelli...@gmail.com> schreef in berichtnieuws
892a3f04-5c3c-4b55...@googlegroups.com...

Christoph M. Becker

unread,
Jul 30, 2016, 8:48:42 AM7/30/16
to
On 30.07.2016 at 14:36, R.Wieser wrote:

> #2. Yes, they are. Data that has been send by the GET method will be stored
> in the $_GET array, and data that has been send by the POST method will be
> stored in the $_POST array.

Small correction: if a POST request contains a query string in the URI,
the $_GET array will be filled with this information.

--
Christoph M. Becker

R.Wieser

unread,
Jul 30, 2016, 9:36:46 AM7/30/16
to
Christoph,

> Small correction: if a POST request contains a query string in
> the URI, the $_GET array will be filled with this information.

And although that briefly went thru my mind, in the end I fully forgot to
mention it. :-\

Good catch!

Regards,
Rudy Wieser


-- origional message:
Christoph M. Becker <cmbec...@arcor.de> schreef in berichtnieuws
nni7n6$l0n$1...@solani.org...

Jivanmukta

unread,
Jul 30, 2016, 1:27:30 PM7/30/16
to
Alla wrote:
> // else if the user is already at that form page and sees the form he
> needs to fill in else if ($_SERVER["REQUEST_METHOD"] == "POST")
> {
> // validate submission
> if (empty($_POST["username"]))
> // do something

BTW, I have a question: how to write conditions "is screen_field empty?". If
I write empty($_POST['screen_field']) I will receive TRUE for
$_POST['screen_field'] === '0'. If screen_field is text input this makes a
problem. Thus I wrote a function:

function is_empty($val) {
if ($val === '0') {
return FALSE;
}
if (is_string($val)) {
$val = trim($val);
}
return empty($val);
}

and I use it for screen fields and database text values but I was criticised
by PHP programmers that my solutions is not "PHP-way".

(Sorry for my English)

Jerry Stuckle

unread,
Jul 30, 2016, 5:18:39 PM7/30/16
to
See isset().


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Alla

unread,
Aug 2, 2016, 3:16:10 AM8/2/16
to
<snip>
Thank you very much for your answers and explanation!
0 new messages