Jump to content

Get vs Post


doubledee

Recommended Posts

Or am I oversimplifying things?

 

A little.  There are semantics behind GET and POST. 

 

Most requests are GET, as all your doing is requesting a document from the server basically.  The server finds it and gives it to you.

 

POST is used for when you need to send information to the server, and the server does something with that information such as save it do a db or modify a file or something, then it will return a result to you.  The only way to do a POST request is via a <form method="post"> tag (or javascript + xhr).

 

 

On a basic level, anything other than submitting form is likely a GET request.  Submitting a form will depend on what the method attribute is set it, and it defaults to get.

 

If you need to decide which method to use, do so based on whether the request will be modifying the data on your server.  If all you'll be doing is serving up a static file, or only showing information from your db but not changing it, use a GET.  If you'll be using the information submitted to make changes to your data, use a POST.

 

Reading this page will give you more info about the methods and how they are intended to be used.

 

 

 

Link to comment
Share on other sites

kicken,

 

The context of my question is wanting to be sure the following code comments are accurate...

// *************************************************************
// HANDLE FORM.						 *
// *************************************************************
if ($_SERVER['REQUEST_METHOD']=='POST'){
// Form was Submitted (Post).


}else{
// Form was not Submitted (Get).
// Drop through to display Form.
}//End of HANDLE FORM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


</body>
</html>

 

I'm not sure if my comment below the ELSE is correct?!

 

 

Debbie

 

 

Link to comment
Share on other sites

The comments are fine.  Your else block is unnecessary though, you could just remove it entirely.  An if statement does not have to have a corresponding else.

 

I know, but I often code like that for symmetry.  (Having a matching pair of logic helps me to figure things out better.)

 

For example...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)==1){
// Member was Found.

}else{
// Member Not Found.
}

 

and

 

// ****************************
// Attempt to Log-In Member.	*
// ****************************
if (empty($errors)){
// Valid Member Record.

}else{
// Invalid Member Record.
// Drop through to display Form.
}

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

right but it has to parse that else every time, even though it is not doing ANYTHING.

what i do, is comment the else:

 

<?php
if($x == $y) {
do something();
} // else { x did not == y}

?>

 

you can even comment multiple lines, at least then it's not trying to parse an empty else statement.

Link to comment
Share on other sites

When my HTML Form is displayed, is that a "GET"??

And when my HTML Form is submitted, is that a "POST"??

Or am I oversimplifying things?

Debbie

 

I'll take a crack at elaborting on your question, just for the sake of completeness and perhaps future googlers. 

 

Everything in HTTP is driven by requests.  The browser is the client and it makes requests to the server, which then provides a response.

 

Client (Request) ---->  Server

Client 

 

So for every request you get a response.

 

This is all described in the RFC for the HTTP protocol.  At this point all modern clients (browsers and HTTP aware applications) support HTTP 1.1.  The wikipedia page that kicken linked you to, lists the types of Requests that are supported.

 

In the case of both GET and POST type requests, a url gets provided, which is how the server determines what resource (ie script) should field that request.

 

URL's are allowed to have query strings attached (everything after the ?).  Of course those querystrings specify variables in name=value pairs.

 

From the php side, any of these url parameters are converted into "$_GET" variables.

 

Now when you look at an HTML form, one of it's parameters is METHOD=.  This can be either "GET" or "POST".  It should be fairly apparent, that if you specify "get" the form elements will be transformed into url parameters. 

 

If however, you specify "post", the variables go somewhere else. 

 

Going back to the original idea of HTTP request/response, each HTTP request has a special data section called the "header" where variables can be provided.  This is where url is specified, the hostname of the server, the cookie data and various other control and configuration variables can be sent.  the wikipedia page lists these in detail. 

 

When you specify a POST, what the client sends in the header is typically these extra header fields:

 

Content-Length: 89
Content-Type: application/x-www-form-urlencoded

 

The Content-Length would be the length of the data being sent in the post, and the Content-Type indicates the format of the data that will follow.  After this header, there's a linebreak, and then the client will send the rest of the data, which typically takes a form similar to a querystring:

 

Let's say you have this form (right off the w3c html spec page):

 



</pre>
<form method="post">
    

    First name: 

    Last name: 

    email: 

     Male

     Female

    
    
</form>
<br><b

 

 

 

You enter in some data and submit the form ---

 

 

firstname Fred

lastname Flinstone

email: fred@bedrock.com

sex - (male chosen)

 

, then look at the Net tab in firebug, and open up the request:

 

Content-Length	67
Content-Type	application/x-www-form-urlencoded

 

And the raw post data that was sent after the header will look like this:

 

firstname=Fred&lastname=Flinstone&email=fred%40bedrock.com&sex=Male

 

In conclusion, a get parameter is really just the header.  There is no other data.  A Post has the header, but it also is followed by some variable amount of data, which the server reads in, and depending on the encoding header, will parse up in various ways.

 

PHP makes this section of data available in the $_POST and $_FILES superglobals, but its raw form is based on what the client sends to the server.

 

Link to comment
Share on other sites

gizmola,

Interesting response, but what about my code in Reply #2?

 

Yes, that variable will indicate if it's a GET request or a POST request.

 

But the $10,000 question is, "In this code below, when I hit the ELSE branch, is my Form being server up using the GET??"

}else{
// Form was not Submitted (Get).

 

 

Debbie

 

Link to comment
Share on other sites

But the $10,000 question is, "In this code below, when I hit the ELSE branch, is my Form being server up using the GET??"

}else{
   // Form was not Submitted (Get).

 

Per the manual for $_SERVER

'REQUEST_METHOD'

    Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.

 

Your if() condition simply checks to see if the request method was 'POST'. There are three other options, so in the else{} block you cannot definitively assume that the request method was 'GET'. But, for most purposes it will be 'GET'

Link to comment
Share on other sites

Did you actually read my post or just scan it?  There is no concept on the server side of a request.  Servers only provide responses.  It is the client that specifies the type of the request.  What the server returns is results... typically the html, or in some cases data of some form (when an image is requested for example). 

Link to comment
Share on other sites

Did you actually read my post or just scan it?

 

I read it in great details.  And guess what?  It didn't answer my original question.  (It just talked around it.)

 

 

There is no concept on the server side of a request.  Servers only provide responses.  It is the client that specifies the type of the request.  What the server returns is results... typically the html, or in some cases data of some form (when an image is requested for example).

 

If my Form is submitted, then this is true...

// *************************************************************
// HANDLE FORM.							*
// *************************************************************
if ($_SERVER['REQUEST_METHOD']=='POST'){
	// Form was Submitted (Post).

 

...because the Client submitted the Form to the Server using a POST method.

 

But my question is what happens when the Form is NOT submitted and therefore there in NO POST but just a request from the Server to load the page...

 

Why are you snapping at me when you didn't answer that?

 

I don't know if the Form not be POSTED and the Client just asking the Server to load the webpage constitutes a GET like in my ELSE...

}else{
	// Form was not Submitted (Get).
	// Drop through to display Form.
}//End of HANDLE FORM

 

 

Debbie

 

Link to comment
Share on other sites

How can I make it clearer? 

 

When my HTML Form is displayed, is that a "GET"??

And when my HTML Form is submitted, is that a "POST"??

 

It is neither.  The php script runs on the server.  It is for all intents and purposes equivalent to the server. 

 

Thus it only returns responses.

Link to comment
Share on other sites

But my question is what happens when the Form is NOT submitted and therefore there in NO POST but just a request from the Server to load the page...

 

If someone clicks a link or just enters the URL in the browser it will be a GET request that is sent to your server.

 

Why are you snapping at me when you didn't answer that?

 

I don't know if the Form not be POSTED and the Client just asking the Server to load the webpage constitutes a GET like in my ELSE...

 

If the request is not a POST, it obviously has to be something else.  Given the information that was provided in various replies as well as the linked Wikipedia page, GET would be the obvious choice.  It could of course be some other request type such as HEAD or PUT, but that is not typical.

 

You could always just var_dump($_SERVER['REQUEST_METHOD']);, load the page, and see for your self what it is.

 

Ultimately you should not really worry as much about whether the page was sent via a POST or a GET, but instead focus on 'was the form submitted or not'.  That is a question more easily answered by checking for specific form details rather than the request method.

 

if (isset($_POST['someFieldInYourForm'])){
   //form was submitted
}
else {
   //form was not submitted
}

 

Link to comment
Share on other sites

Ultimately you should not really worry as much about whether the page was sent via a POST or a GET, but instead focus on 'was the form submitted or not'.  That is a question more easily answered by checking for specific form details rather than the request method.

 

if (isset($_POST['someFieldInYourForm'])){
   //form was submitted
}
else {
   //form was not submitted
}

 

So just drop the (POST) and (GET) from my comments and I should be okay?

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.