doubledee Posted February 4, 2012 Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/ Share on other sites More sharing options...
kicken Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314476 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314481 Share on other sites More sharing options...
kicken Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314489 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314492 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314499 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 right but it has to parse that else every time, even though it is not doing ANYTHING. True, but my script isn't a 3-D Video Game or Real-Time Stock Trading System, so I don't think the extra "overhead" is anything to sweat?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314505 Share on other sites More sharing options...
gizmola Posted February 4, 2012 Share Posted February 4, 2012 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: [email protected] 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314517 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 gizmola, Interesting response, but what about my code in Reply #2? Are my comments consistent with what everyone is saying? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314531 Share on other sites More sharing options...
gizmola Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314574 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314577 Share on other sites More sharing options...
Psycho Posted February 4, 2012 Share Posted February 4, 2012 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' Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314580 Share on other sites More sharing options...
gizmola Posted February 4, 2012 Share Posted February 4, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314582 Share on other sites More sharing options...
doubledee Posted February 5, 2012 Author Share Posted February 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314641 Share on other sites More sharing options...
gizmola Posted February 5, 2012 Share Posted February 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314650 Share on other sites More sharing options...
kicken Posted February 5, 2012 Share Posted February 5, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314670 Share on other sites More sharing options...
doubledee Posted February 5, 2012 Author Share Posted February 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256401-get-vs-post/#findComment-1314677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.