I am a beginner with PHP and I would really like to understand it a lot better. I know you can use $_POST to retrieve information from a form by setting method="$_POST". But why does this work exactly? Is, upon form submission, an array named $_POST created? Does the array then contain all of the element values from the form?
Asked
Active
Viewed 2,537 times
3
-
3Question is too vague and general. Read up on it on the PHP manual (a great manual) try it out by writing your own code and then come back here if you have a programming question. – markus Mar 03 '12 at 19:12
-
1You might be better off consulting the [PHP manual](http://uk.php.net/manual/en/language.variables.external.php) – richsage Mar 03 '12 at 19:14
-
8seems a perfectly valid question to me.. it's not vague at all – Karoly Horvath Mar 03 '12 at 19:22
-
This is a valid question and one that many beginners might question. From their view, it just "magically" happens, once you insert `post` into the method for a form. Its only natural that they want to know the inner workings. – Games Brainiac Mar 15 '13 at 06:46
-
I think this is a good question as the answer told me exactly what I needed to know. – Daniel Katz Aug 20 '16 at 17:01
1 Answers
8
When you press the submit button, your client will send a HTTP POST request to the script specified in action
. In your situation, the values filled in the form will be sent as parameters in this request (note that a client can potentially send anything in a GET/POST request; this is important to know for security). PHP will put those parameters in the $_POST
superglobal so the script can do something with the form data.
Forms aren't the only source of POST data, both in theory and in practice. For example, an API could accept HTTP POST requests, and Javascript can let a browser perform passive asynchronous requests using AJAX.

Another Code
- 3,083
- 21
- 23