PHP Forms

Main | Review of Operators | Mathematical Functions | Practice 5 | Solution 5 | HTML Forms | PHP Forms | Practice 6 | Solution 6 | PHP and E-mail | Practice 7 | Solution 7

You should now you 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. [Remember, variables are set to values with the assignment operator (=). 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.

  1. $var;
    isset($var) == F *
    empty($var) == T
    

  2. $var = "";
    isset($var) == T *
    empty($var) == T **
    

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

In this tutorial we will use these functions to show the correct text-boxes and responses at the appropriate time. In our examples, there will be three states to online form presentation:

  1. When the user first arrives we want to show them only the text boxes and buttons and ask the user to fill them out and click on the "Submit" button. To implement this behavior we will use the function isset(), since the variables for our online form will not be set when the form is first loaded.

  2. After the user fills out the form and submits the information, we want to do a check to make sure that the user has not made any mistakes or left any fields empty. To implement this behavior we will use the function empty() to look for the case in which the user left a text-box empty. If a user did leave a text-box empty we can echo a message to them, asking them to enter text in the box that they left blank, and redisplay the form. We can also echo the text that the user had originally entered into the other text-boxes, so the user will not have to re-type his/her entries (this method of saving the user the trouble of retyping is called making the form "sticky").

  3. If the user has filled out the form correctly we want to echo a message thanking him/her, and process the data. To implement this behavior we will use the function empty(), since we want to conduct a test to make sure that the variable is not empty.

We will also define our own function called show_form(), which displays the HTML to make our form. Our program can listen for one of the above three states and then call show_form() with the relevant arguments for each case. Our future examples will use variations of show_form(). In this example show_form() is defined as the following:

<? function show_form($value="") {  ?>
  <form action="submit.php" method="post">
  <input type=text name=value 
	value="<?echo $value?>">
  <input type=submit>
  </form>
<? }  

Note how this function switches between HTML and PHP. Right in the middle of defining our function we switch back to HTML to avoid the use of numerous echo statements for simple HTML. Then, when we need a PHP variable we switch back to PHP to echo the variable $value. The action field of the HTML specifies what will happen when the "submit" button is pressed. In this case the form will post to a file called "submit.php3", which is the one file that this entire application is stored in. Thus, this application sends data to itself, and handles the data in different ways depending on the status of its variables, which it can check with isset() and empty(). Our function can also take optional arguments which become the default values when the form is loaded. These optional arguments relate to the "three states to online form presentation" as discussed above in the following ways:

  • In state 1 the variable $value is not set. The function however is defined so that it can be called without arguments (as discussed in our previous section on functions). This design decision avoids the "Missing argument" error message that would occur otherwise. The text-boxes will appear blank in this state since $value will be empty.

  • In state 2 the variable $value will be set by the user, and will be echoed in the appropriate text-box, so the user will not have to retype his/her entry.

  • In state 3 we do not need to display our form, so our function is not called.

Here is a PHP application which uses these ideas and echoes its own code along with comments on how it works as it runs. This application should make clear exactly how the ideas from our discussion work together.

You should now have a clear understanding of isset() and empty(). We will use these functions in the rest of our examples for this tutorial. For supplementary information on these functions, see the online PHP manual's documentation on isset() and empty().


jfulton [at] member.fsf.org
22 Aug 2013