Tutorial 6 : Forms Global and Environment Variables Before you actually build a form and use it to acquire data, you need to make a small detour and look again at global variables. You first met these in Hour 6, "Functions." A global variable is any variable declared at the "top level" of a script— that is, declared outside a function. All functions are made available in a built-in associative array called $GLOBALS. <?php $user1 = "Bob"; $user2 = "Harry"; $user3 = "Mary"; foreach ( $GLOBALS as $key=>$value ) { print "\$GLOBALS[\"$key\"] == $value<br>"; } ?> We declare three variables and then loop through the built-in $GLOBALS associative array, writing both array keys and values to the browser. In the output, we were able to locate the variables we defined, but we saw an awful lot more besides these. PHP automatically defines global variables that describe both the server and client environments. These are known, therefore, as environment variables. According to your system, server, and configuration, the availability of these variables will vary, but they can be immensely useful.
A simple HTML form <html> <head> <title>simple HTML form</title> </head> <body> <form action="form.php" method="GET"> <input type="text" name="user"> <br> <textarea name="address" rows="5" cols="40"> </textarea> <br> <input type="submit" value="hit it!"> </form> </body> </html> We define a form that contains a text field with the name "user", a text area with the name "address", and a submit button. The FORM element's ACTION argument points to a file called form.php, which processes the form information. Because we haven't added anything more than a filename to the ACTION argument, the file form.php should be in the same directory on the server as the document that contains our HTML. Reading Input from the Form <html> <head> <title> Reading input from the form</title> </head> <body> <?php print "Welcome <b>$user</b><P>\n\n"; print "Your address is:<P>\n\n<b>$address</b>"; ?> </body> </html> In the code, we have accessed two variables, $user and $address. It should come as no surprise that these variables contain the values that the user added to the text field named "user" and the text area named "address". Forms in PHP4 really are as simple as that. Any information submitted by a user will be available to you in global variables that will have the same names as those of the form elements on an HTML page. Distinguishing Between GET and POST Transactions To work flexibly, a script that can accept data from any source must be able to decide whether to read the $HTTP_GET_VARS or $HTTP_POST_VARS arrays. On most systems, you can discover whether you are dealing with a GET or POST transaction in the environment variable $REQUEST_METHOD, which should contain the string "post" or "get". To be absolutely sure that your scripts are entirely portable, however, you can take advantage of the fact that the $HTTP_POST_VARS array will only be present if a POST request has been made. Extracting Parameters from Either a GET or POST Request <html> <head> <title> GET or POST request</title> </head> <body> <?php $PARAMS = ( isset( $HTTP_POST_VARS ) ) ? $HTTP_POST_VARS : $HTTP_GET_VARS; foreach ( $PARAMS as $key=>$value ) { if ( gettype( $value ) == "array" ) { print "$key == <br>\n"; foreach ( $value as $two_dim_value ) print ".........$two_dim_value<br>" } else { print "$key == $value<br>\n"; } } ?> </body> </html> We use the ternary operator to set a variable called $PARAMS. Using the built-in isset() function, we first check whether the $HTTP_POST_VARS array has been set. isset() returns true if the variable it is passed has been defined. If the $HTTP_POST_VARS array has been defined, the ternary expression resolves to this; otherwise, it resolves to $HTTP_GET_VARS. We can now use the $PARAMS array throughout the rest of the script without worrying about whether it has been populated as the result of a GET or a POST request.
continued to next page... |