tags:

views:

72

answers:

7

Can any one please tell whats going on in this program? My one main doubt is about the 1st condition from where will we get the method for REQUEST_METHOD i mean the program is gng in the 1st if loop so REQUEST_METHOD == GET but where are we setting it.

<html>
<head><title>Temperature Conversion</title></head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') 
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
Fahrenheit temperature:
<input type="text" name="fahrenheit" /> <br />
<input type="submit" name="Convert to Celsius!" />
</form>
<?php
} 
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    $fahr = $_POST['fahrenheit'];
    $celsius = ($fahr - 32) * 5/9;
    printf("%.2fF is %.2fC", $fahr, $celsius);
} 
else 
{
    die("This script only works with GET and POST requests.");
}
?>
</body>
</html>

Once again I just say Thanks

A: 

It's an odd method of utilizing the same php file for display and processing post requests.

  1. The developer assumes the first pass of the page will be a "GET" request for the page
  2. Then someone fills out the form and posts back to the same php file where the value is processed and the temperatures are printed out to the screen
  3. It also catches if someone makes another type of call to the page, such as a "head" request.
Myles
It's really not that odd, just old. It was really common style to do this in php3 and really early 4. But yeah, now it's really weird.
Chuck Vose
When I was working in those versions I used a separate php file to handle the action, instead of overloading a page like this.
Myles
Heh, maybe it was just me. I won't pretend that I was a great programmer back then :)
Chuck Vose
+1  A: 

GNG? (EDITED: from the question quote: i mean the program is gng in the 1st..)

REQUEST_METHOD will be GET if you enter the page by url.

When you submit the form, form method state method=POST, so by submitting to itself, REQUEST_METHOD will be POST.

joetsuihk
is it something that we set that value by default? i mean REQUEST_METHOD == GET
Josh
@joetsuihk: GNG?! @Nishant: No, it's *always* GET.
Alix Axel
No, its set by the server.
Brad
And no, its not always GET! It depends on what HTTP method was used to access the file.
Brad
@Alix Axel: in the question: i mean the program is gng in the 1st.. and browser set the method to GET by default when you enter an url to it.
joetsuihk
A: 

Its set by the original request. The server populates this information (ie. $_SERVER) for you so that you have access to request and server data from within PHP. If the request comes in by an HTTP GET, the value is GET. If its POST, the value is POST, etc.

Brad
A: 

The request method refers to the method used to "request" the page by the browser.

If a page is accessed simply by navigating to a URL, the GET method is used. Hence, if the page you list is just browsed to in your browser, REQUEST_METHOD == GET and the page displays the form.

The POST request method is commonly used when a page is accessed via a form submission. Thus, once the user fills in the form on your page and submits it, they return to the same page but via the POST request method. Then the page displays a bit of info based on the form submission.

See Request Methods at Wikipedia for more info on request methods. See the PHP docs on $SERVER for more info on server variables.

ngm
A: 

This program handles an HTTP request. The $_SERVER array is automatically set with values.

$_SERVER['REQUEST_METHOD'] will equal 'GET' if somebody just visits the page in a browser.

It will equal 'POST' if they get to the page after submitting a form.

Drew LeSueur
A: 

GET and POST are the http verb's used to access the page. Usually we use POST to send user data back to the server and use GET to, well, get data from the server. So asking for an index.html is a GET request but POSTing data to something like a comment form, sending data back to the server, uses POST.

This is one of the ways of using only one file to do form processing in php. When the person first accesses the form they'll be using GET. But when the user submits data and click the Submit button it'll send the user data back in a POST. Thus the first if condition == 'GET'.

Try this link to learn more about GET and POST

Chuck Vose
A: 

Ok. So, because I've only seen 1 person come out and say it, I guess I will have to:

$_SERVER, $_POST, $_GET and most other variables that begin with $_ are called 'predefined variables' or 'superglobals'. These are variables that are set by the server and can be access in any context. They store information based on the server runtime, the request, etc.

$_SERVER['REQUEST_METHOD'] defines the method that the user got to your page. There are a bunch such as HEAD, GET, POST, DELETE, and I believe a few more. Each one of them has a specific purpose as lined out by internet HTTP standards. GET happens to be the request method that is used the most often, as it is used to simply get a page. If you haven't submitted a form, chances are this is what method you are using.

What your page is doing is checking that the user is browsing to the page regularly, and if they are it gives them a form. When you press the submit button on a form, you change your request method to POST for the next page, meaning that you are giving some sort of information to the server, in this case the data that is in the form fields.

If the page detects that someone is sending a form, it then goes and makes calculations based off of what the user sent through the form.

Generally most people just check if there is any data contained in $_POST to tell if a form has been sent through, but this method works as well.

Chacha102