Variables from outside PHP
HTML Forms (GET and POST) - Please leave a comment after reading.
When a form is submitted to a PHP script, in this case foo.php, the information from that form is automatically made available to the script. The information in the form can be accessed in the script foo.php for example:
Depending on your particular setup and personal preferences, the information in the form can be accessed in the script foo.php from your HTML forms. Some examples are:
Try the above example:
Enter the details in the form and click the Submit button. This page will post to( be redirected ) to the file file.php which contains the php code exactly as typed above in code 2.
Using a GET form is similar except you'll use the appropriate GET predefined variable instead. GET also applies to the QUERY_STRING (the information after the '?' in a URL). So, for example, http://www.example.com/test.php?id=3 contains GET data which is accessible with $_GET['id'].
Note: $_POST and $_GET, became available in PHP 4.1.0
PHP also understands arrays in the context of form variables . You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data:
More complex form variables
|
To see the above code in action just use the form below. The $_SERVER['PHP_SELF'] variable causes this page to post to itself with any PHP code on it to be executed such as that which is above the form code in the above example. To see the result of this code enter some details and hit the submit me button. You can multiple select in the Beer textbox by holding down the Ctrl key.
In PHP 3, the array form variable usage is limited to single-dimensional arrays. As of PHP 4, no such restriction applies.
IMAGE SUBMIT variable names
When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:
<input type="image" src="image.gif" name="sub" /> |
When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y. These contain the coordinates of the user click within the image. The experienced may note that the actual variable names sent by the browser contains a period rather than an underscore, but PHP converts the period to an underscore automatically.
HTTP Cookies
PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the setcookie() function.
If you wish to assign multiple values to a single cookie variable, you may assign it as an array. For example:
<?php |
That will create two separate cookies although MyCookie will now be a single array in your script.
Note that a cookie will replace a previous cookie by the same name in your browser unless the path or domain is different. So, for a shopping cart application you may want to keep a counter and pass this along. i.e.
Dots in incoming variable names
Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:
<?php |
For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.
