Using Array and Array

   

Using $HTTP_GET_VARS and $HTTP_POST_VARS

PHP automatically registers[1] any variables passed from a form into the $HTTP_GET_VARS or $HTTP_POST_VARS array. If the form is submitted with the post method, then any values in the form are assigned to the $HTTP_POST_VARS array. If the form is submitted using the get method (the default method if no method is specified), then the values of the form are assigned to the $HTTP_GET_VARS array.

[1] Your php.ini file is automatically enabled for this feature if you are using PHP 4.0.3 or greater. If it is not turned on, then you have to edit the line in php.ini for track_vars and set it to true.

For example, when the following form is submitted to a PHP script:

 <form action=script.php method=post>  <input type=hidden name=firstname value="Edward"> <input type=hidden name=occupation value="Computer Specialist"> </form> 

PHP automatically creates an array that can be accessed associatively:

 $PHP_POST_VARS['name'] = "Edward";  $PHP_POST_VARS['occupation'] = "Computer Specialist"; 

This functionality makes it easier to create and manage form functions, as you don't have to either globalize each variable or list each variable when you call the form handler function.

If you had a form that called for the input of 15 separate fields and did not use $HTTP_POST_VARS or $HTTP_GET_VARS, then your function definition might look something like:

 function my_form_handler($var1, $var2, $var3, $var4,  ..., $var19, $var 20){ 

or:

 function my_form_handler(){    global $var1, $var2, $var3, $var4, ..., $var19, $var 20; 

Using $HTTP_POST_VARS or $HTTP_GET_VARS, your code is more manageable:

 function my_form_handler($HTTP_POST_VARS) {  

or:

 function my_form_hander() {  global $HTTP_POST_VARS; 

This also makes it easier to add (or subtract) additional fields to your forms without having to search through your code for the function calls that handle the form and the additional variables to the function call line.

An example:

Script 4-3 form2.php
  1.  <?  2.  function print_form() {  3.    ?>  4.    <form action="form2.php" method=post>  5.    <p>First Name: <input type="text" name="first">  6.    <br>Last Name: <input type="text" name="last">  7.    <br>Number: <input type="text" name="number">  8.    <br><input type="submit" name="submit" value="Submit">  9.    </form> 10.    <? 11.  } 12. 13.  function form_handler($first, $last, $number) { 14.    $sql = "insert into phonelist values ('$first', '$last', '$number')"; 15.    echo "<P>$sql"; 16.  } 17. 18.  function form_handler2($HTTP_POST_VARS) { 19.    $sql = "insert into phonelist values('" . $HTTP_POST_VARS['first'] . 20.                        "','" . $HTTP_POST_VARS['last'] . 21.                        "','" . $HTTP_POST_VARS['number'] . "')"; 22.    echo "<P>$sql"; 23.  } 24. 25.  if(isset($submit)) { 26.    form_handler($first, $last, $number); 27.    form_handler2($HTTP_POST_VARS); 28.  } else { 29.    print_form(); 30.  } 31.  ?> 

Script 4-3. form2.php Line-by-Line Explanation

LINE

DESCRIPTION

2 11

Declare a function, print_form(). This function simply prints a form so that the user can enter a first name, a last name, and a phone number.

13

Declare a function, form_hander(), that requires three arguments, the items returned when the user submits the form from the print_form() function.

14

Generate the SQL using the values provided by the user.

15

Print out the generated SQL.

16

End the function.

18

Declare a function, form_handler2(), that requires only one argument, HTTP_POST_VARS.

19 21

Generate the SQL using the values provided by the user.

22

Print out the generated SQL.

23

End the function.

25

If the user has pressed the "Submit" button, then execute the script until line 27.

26

Call the form_handler() function with the three required variables.

27

Call the form_handler2() function with just $HTTP_POST_VARS.

28 30

If the "Submit" button has not been pushed, then execute the print_form() function.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net