Sticky Forms
Introduction.
When you make use of a form on the web you may find that sometimes after
submitting information and being told to go back and correct it you are
sent back to an empty form! This is extremely irritating. If
you are sent back to a form that retains the information you already
entered you are said to be working with a sticky
form. Another very typical situation where sticky forms are important is when a user accesses something in a web database online using a web form. In order to fill in the form the programmer must know how to implement sticky forms. We will do this in more than one way when developing our user profile management interface and our administrative user profile management interface. Unfortunately, Ullman's examples are only configured with text boxes. In reality, it is also very important to be able to work with text areas, check boxes, radio buttons and select/option boxes. Thus our examples, regardless of how contrived they might seem, will eventually make certain you work with all of these form controls. Just as when working with ASPs and JSPs, PHP usually becomes the middleware that controls the interactions between HTML and whatever database management system is being used. The tricks involved in doing this can be quite complicated as we will see in future web pages. A First Example. For our first example we will work a simple problem that takes a user's name, e-mail address and password. We will also require the user to input their password a second time to increase the likelihood they know what they entered. We will do some input validation to make certain that everything is entered and checks out. In order to obtain information from the form you can use the naming conventions we have used so far. You can also use one the
arrays referenced by the name of the control you are looking for. You should call this script register.php. |
<html> <head> <title>A Form Page that Processes Itself</title> </head> <body bgcolor="003044" text="cccccc"> <?php // making sure the form has been submitted if (isset($_REQUEST['cmd_submit'])) {
}
<?php |
Notice that the way we get the form to post to itself
makes use of
<form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method = "post"> Notice how there is PHP code embedded in the form tag. This code makes certain at least something is entered for the name and e-mail address. It also compares the entries for the password and makes sure they are identical. Error messages are accumulated and actually presented on the same page without representing the form. Another Example. Now we will adjust the previous example to make it sticky so that the form is presented along with the messages about the user's input errors. There isn't a lot of differences other than the form is presented regardless of the user's inputs and can be more easily modified based on the commentary above it. You should call this file register_sticky.php. Because the form is always presented the logic is actually a bit simipler. But now you can focus on how the entries in the form are filled in. |
<html> <head> <title>A Form Page that Processes Itself</title> </head> <body bgcolor="003044" text="cccccc"> <?php // making sure the form has been submitted if (isset($_REQUEST['cmd_submit'])) {
} |
I have highlighted how the value is used in the HTML
tag for the text boxes to obtain the posted values using the $_REQUEST
array. value = "<?php if (isset($_REQUEST['txt_name'])) echo $_REQUEST['txt_name']; ?>" value = "<?php if (isset($_REQUEST['txt_email'])) echo $_REQUEST['txt_email']; ?>" These values actually contain some PHP code that displays what was entered again using the echo function. We will work with the other form controls in the following webpage. |