Monday, December 4, 2006

Access Form Variables

An aim of using php for you may be processing html forms. its easy to access form variables in php. i wont write a sample html form code here. i suppose you already have an html form where each input has a name value. There are general 3 ways of accessing form variables in php. Lets suppose that you have an input field in your form named as input_var :

$input_var //short style
$_POST['input_var'] //middle style
$HTTP_POST_VARS['input_var'] //long style

the short style is valid only if the register_globals configuration is on. since php version 4.2.0 this configuration is off as default. this style is not recommended.

middle style is the recommended style that is valid since php version 4.1.0

long style was portable in the past but now requires the register_long_arrays configuration.

i always use the middle style. it is easy, not confusing and also portable. if you dont want to access your form variable like this everywhere needed in your code then you can just assign it to another variable (with the form name generally) and use it freely as you need.

$input_var = $_POST['input_var'];
// lets use our form variable for output
echo $input_var;