Languages - PHP
GET and POST
There are 2 ways to pass
form parameters
back from a web page
Get
| The parameters are displayed in the URL
|
---|
Post
| The parameters are hidden from the users
|
---|
Overview
| GET example
| POST example
| Some Functions
| Radio buttons
| Checkboxes
Overview
Both
$_GET
and
$_POST
are super globals -
meaning that they are available at all times, in any subroutine,
without having to declare them as
globals.
In all cases, you should never assume that a parameter exists.
To prevent errors like
Notice: Undefined index: num_of_records in C:\path\Some_file.php on line 15
|
you should always test for the existence of a parameter before
attempting to use it.
if (isset($_GET['num_of_records'])) {
$mc_num_of_records = $_GET['num_of_records'];
} else {
$mc_num_of_records = 10;
}
if (isset($_POST['num_of_records'])) {
$mc_num_of_records = $_POST['num_of_records'];
} else {
$mc_num_of_records = 10;
}
| |
I tend to develop forms using get
(because it is easier to debug what you can see),
and to publish them using post
(so the users can't manipulate them and break something).
However, the
debug parameters
are always retrieved
using get so that I can control those manually
(ie, type them in).
In order to make it difficult for others to see your debug stream,
you can
- Disable the feature in production code
- Have a URL level debug password
GET example
With
Get,
the parameters are visible in the url.
URL |
http://some.system/some_file.php?num_of_records=200&debug
|
PHP code that creates and reads the parameters
POST example
With
Post,
the parameters are not visible in the url.
URL |
http://some.system/some_file.php
|
PHP code that creates and reads the parameters
Some Functions
It is absolutely required (or at least - good practice)
to make sure that the variable is defined and that the
input parameter exists.
$ca = ''; // must be initialized
if (isset($_POST['CurrentlyActive'])) {
$ca = $_POST['CurrentlyActive'];
}
if ($ca == 'a'){} // fails is $ca is not initialized and not found
|
If you just need a value,
the following functions perform the appropriate tasks.
function mc_get_url_value($field, $default=''){
if (isset($_GET[$field])) {
$t = $_GET[$field];
} else {
$t = $default;
}
return $t;
}
function mc_get_post_value($field, $default=''){
if (isset($_POST[$field])) {
$t = $_POST[$field];
} else {
$t = $default;
}
return $t;
}
function mc_get_passed_value($field, $default=''){
if (isset($_GET[$field])) {
$t = $_GET[$field];
} else if (isset($_POST[$field])) {
$t = $_POST[$field];
} else {
$t = $default;
}
return $t;
}
$ca = mc_get_post_value('CurrentlyActive');
if ($ca == 'a'){} // Now $ca is always initialized
|
With the 3rd function, you won't have to change your code
when you just change the form parameter.
Radio buttons
All the buttons in a radio group have the same name.
The value returned indicates which button is selected.
This example keeps the current radio button checked
when the form is recalled.
Using the functions defined above, the following would simplify
handling radio buttons.
Checkboxes
Check Boxes are not associated into groups.
In these cases, the parameter exists only when the box is checked.
This example keeps the keeps the state
when the form is recalled.
Using the functions defined above, the following would simplify
handling checkboxes.
Author:
Robert Clemenzi