Reputation: 117
I understand if I create an html form with the action attribute set to a PHP script, I can access the POST data via the super global $_POST. My question is how does this happen?
If I have a simple form like this:
<form action="script.php" method="POST">
<input type="text" name="fname">
<input type="submit" value="Submit">
</form>
And a simple script like this:
<?php
echo $_POST["name"];
?>
What mechanism brings the raw form data from the POST request into PHP? Is there some sort of CGI Script? What would the equivalent be for another language, say Ruby?
Upvotes: 0
Views: 1106
Reputation: 33413
When you submit a form through the browser, the browser will take the data and serialize it, which then gets send to the web server in a HTTP/HTTPS request.
For GET forms the data is serialized and appended to string query in your URL e.g.myphp.php?field1=foo&field2=bar
For POST data it is inserted in the body of the request.
Based on the type of the request PHP engine will then parse the data received and for your convenience url decode it and populate the superglobals with it. In the example above the data will be like this:
$_GET = [
'field1' => 'foo',
'field2' => 'bar'
];
This is a nice feature of PHP in contrast to language like PERL which force you to parse the input data yourself.
Upvotes: 1
Reputation: 1702
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
$HTTP_POST_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_VARS and $_POST are different variables and that PHP handles them as such)
https://www.php.net/manual/en/reserved.variables.post.php
It's just a super global variable that the PHP interpreter stores all post data in until the next request on which it is cleared.
Upvotes: 1