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.
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.
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:
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:
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
|