Understanding the $_POST Let's start with $_POST. Ideally you should use $_POST when:
In their most basic form, PHP web forms take two elements. The first is the HTML web form asking for user input. The second is the processing script that retrieves the posted values to the server. This is where you will be using $_POST to retrieve these values. Basic code example: This is the web form HTML code (basicpost.php):
The web form code above states that when the form is submitted, the processing of the web form posted values will be done by a script named “processor.php”. It also shows that the method of form submission is using the HTTP POST method. This is the code of processor.php:
By placing both files (basicpost.php and processor.php) in the same folder of your test server, you can actually run this form. First, it asks for your name. After typing your name and pressing the “Submit your name” button the value of the “yourname” textbox will be submitted to processor.php. Since webform.php is using HTTP POST, it should be retrieved using $_POST variable, example:
Echo command will display it back to the browser (ie; “Your name is Codex M”). You might observe that after form submission, you will no longer see your name displayed in the URL. IMPORTANT: DO NOT USE THE ABOVE SAMPLE CODE IN YOUR PHP APPLICATIONS. The purpose is just to show an entry/beginner level example on the use of $_POST in a web form. The primary reason is that the above script is NOT SECURE for practical use. Security when using these variables will be discussed later in this tutorial. Why is $_POST a global variable? A global variable is one that you can use and re-use throughout your PHP script even inside a function. For example:
In the above example, $x is NOT a global variable. When used inside function testing(), it won't carry its value, which is 1. When the above code runs, it won't output 1 to the browser. To make the above variable attain a global scope. You need to declare it as “global”:
The above code now outputs 1, because $x value can be used and re-used throughout the script, it's now a global variable. $_POST is a global variable. There is no need to define $_POST as global. This makes it very convenient for programmers to retrieve the value $_POST anywhere in the script as needed. However since it's global it also introduces a lot of security related issues. More of this will be discussed later. Why is $_POST an associated array variable? If you are familiar with an array variable then you might know that to retrieve the value of the array, you should use square brackets such as:
This means that “Codex Meridian” has a pair value of “DevShed.com”. Now when this is accessed using:
The output is “DevShed.com” in the browser. Same thing with $_POST. Supposing you will dump the output of the above basic $_POST example:
This is the output of the var_dump (supposing you will enter “John Doe” as name:
The above var_dump of $_POST shows that it is indeed an associative array variable.
blog comments powered by Disqus |