sas

Topic: PHP & MySQL


PHP With Forms

This page will provide an example login form for authenticating to mysql server. It also establishes a user session, and processes the results of the form. It connects to the database server after the form has been submitted and shows a list of available databases for review.

The section about sessions explains how that part of the code works. The section called "MySQL Example" has the code that allows the user to select a table in the database and display the contents of the table. The listing of the "dblist.php" file is shown in that section which is numbered as item 14.

Processing the Form Data
Now lets create process.php to use the data from the HTML form we made:

<?php
print "Your name is ". $Name;
print "<br />";
print "You are ". $Age . " years old";
print "<br />";
$old = 25 + $Age;
print "In 25 years you will be " . $old . " years old";
?>

As you may be aware, if you leave out the method="post" part of the form, the URL with show the data. For example if your name is Bill Jones and you are 35 years old, our process.php page will display as

http://yoursite.com/process.php?Name=Bill+Jones&Age=35

If you want, you can manually change the URL in this way and the output will change accordingly.

You should now have some notion of how to use HTML to get input from a user. We will now investigate what you can do with PHP once you have that input. For this discussion we will focus on the built-in PHP functions isset() and empty(), and see how they can be used with conditionals. We will then use an interactive PHP application that can display its own code to clarify how all of these ideas work together.
The built-in PHP functions isset() and empty() are different, but the difference between them can be confusing.
  • The function isset($var) will return true if the variable $var has been set to a value. It will return false if $var has not been set to a value. [Variables are set to values with the assignment symbol (=). For example, $x = 5 assigns the value of '5' to $x. ]
  • The function empty($var) assumes that the variable $var has been set to a value. It tests what the value of $var has been to. If $var has been set to the null-string ("") then empty($var) will return true. If $var has been set to any other string it will return false.

To make the definitions of isset() and empty() more rigorous consider the following, which demonstrates the conditions under which they will return True or False, given the values of the variable $var. These examples are meant to demonstrate the truth values that the functions isset() and empty() would return. Not all of the following is valid PHP. The statement "f(x) == F" means the function f of argument x returns False and the statement "f(x) == T" means the function f of argument x returns True. The asterisk (*) is used to call attention to what changes are made, as the value of $var changes.

$var;
isset($var) == F *
empty($var) == T
$var = "";
isset($var) == T *
empty($var) == T *
$var = "x";
isset($var) == T
empty($var) == F **


Variables from Forms

The name that you use for a form will create a variable with the same name in your program. For example, if the following form were submitted:

<input type="text" name="my_var">  

You could access the value from this field with $my_var.

This may not work with all PHP configurations by default. You can only access the variable above if PHP is configured with Register Globals turned on. Regsiter Globals is currently turned on for RCI and Eden.

If Register Globals is turned off you need to explictly get the variable out of the $_REQUEST array like this: $_REQUEST['my_var'].

So, if the values from your forms like the one above do not seem to work, i.e. if the following produces nothing:

echo $my_var;  

Try this:

echo $_REQUEST['my_var'];  

For more information see Using Register Globals and Predefined Variables.


Prev