John W. Holmes wrote:
It is true that a Web client can only use one method in an HTTP request, but this is not as directly related to $_GET and $_POST as it sounds.Can I use both _GET and _POST in the same php file? Thanks!!
Yes, you can use both in your code, but only one method is going to be valid when your page is requested by a user. A user can't request a page with both a GET and POST request at the same time.
There is a sort of naming convention that no one seems to agree on, and that is what to call the different types of data a client can submit. Everyone agrees on cookies, because they are named in a specification, but what do you call variables contained in the query string of a URL? What about variables contained in the content section of a POST request?
As it turns out, different communities use different names. The PHP community, for the most part, uses the term "get variables" to refer to those contained in the query string of a URL. Other communities refer to these as URL variables. The PHP community, again for the most part, uses the term "post variables" to refer to variables contained in the content section of a POST request. Others sometimes call these form variables.
One important point is that these variables are not defined by the type of request in which they are contained. Consider this HTTP request:
POST /index.php?foo=bar HTTP/1.1
The receiving page, index.php, will have $_GET["foo"] defined as well as $_POST["username"]. So, the answer to the original poster's question (as I interpret the question) is yes.
If you are interested in some of the reasons behind the different choices for naming these variables, read on ...
The use of get variables and post variables originates from the valid values of the method attribute of the HTML form tag. When get is specified, the Web client will submit a GET request to the URL identified in the action attribute. In order to accomodate all of the user's data included in the form, the Web client will include this data in the query string of the URL it requests. In fact, if the following HTML is used ...
<form action="/index.php?foo=bar" method="get">
... most browsers will basically overwrite the foo variable defined in the URL with all of the user's data submitted in the form. This is a common pitfall. So, because of this behavior of browsers, variables that are included in the URL in this way are often called get variables. It is more of a reference to the method attribute of the HTML form tag than it is a reference to the HTTP request method. If a method of post is specified, the Web client will use a POST request and include all of the data in the content section of the request, leaving the URL specified in the action attribute untouched. Thus, variables included in the content section of a POST request are often called post variables.
The reason for the term URL variables is simply because these variables are located within the URL. The use of form variables is a bit less intuitive, and some people passionately disagree with this term when used as an equivalent to post variables. The reason for the disagreement is that many developers refer to form variables as any data submitted in a form, regardless of the method being used. However, those that prefer the term form variables are looking beyond HTML. Consider this HTML form ...
<form action="/index.php" method="get">
<input type="hidden" name="foo" value="bar">
... and this HTML link ...
<a href="/index.php?foo=bar">Click Here</a>
When a user submits this form or clicks this link, the request is identical and will look something like this:
GET /index.php?foo=bar HTTP/1.1
Thus, because there is no difference from a technical perspective, these variables are considered URL variables, regardless of whether they were submitted in an HTML form.
ColdFusion developers are more familiar with the URL/form distinction than the get/post one, as the former are variable scopes. PHP developers generally use the get/post distinction due to the arrays $_GET and $_POST. As I understand it, mod_perl developers do not distinguish the two, though I have only heard this from one source (though he is a highly respected member of the mod_perl community). I am less certain about other communities.
That's probably more information than anyone wanted on this topic, but perhaps some people were as curious about this as I once was.
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php