Making Sticky Forms


You've certainly come across sticky forms, even if you didn't know that's what they were called. A sticky form is simply a standard HTML form that remembers how you filled it out. This is a particularly nice feature for end users, especially if you are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).

To preset what's entered in a text box, use its value attribute:

 <input type="text" name="city" size="20"   value="Innsbruck /> 

To have PHP preset that value, print the appropriate variable:

 <input type="text" name="city" size="20"  value="<?php echo $city; ?>" /> 

With this in mind, I'll rewrite calculator.php so that it's sticky.

To make a sticky form

1.

Open calculator.php (refer to Script 3.5) in your text editor.

2.

Change the quantity input to read (Script 3.6)

 <p>Quantity: <input type="text"  name="quantity size="5"  maxlength="10 value="<?php if  (isset($_POST['quantity])) echo  $_POST['quantity]; ?>" /></p> 

Script 3.6. The calculator's form now recalls the previously entered values.


The first thing I've done here is to add the value attribute to the input. Then, I print out the value of the submitted quantity variable ($_POST['quantity']). But first I want to make sure it has a value, so I check that the variable is set. The end result for the input's value is the PHP code

 <?php if (isset($_POST['quantity'])) {   echo $_POST['quantity']; } ?> 

which I've condensed to its most minimal form (you can omit the curly braces if you have only one statement within a conditional block).

3.

Repeat the process for the price and tax.

 <p>Price: <input type="text"  name="price size="5"  maxlength="10 value="<?php if  (isset($_POST['price])) echo  $_POST['price]; ?>" /></p> <p>Tax (%): <input type="text"  name="tax size="5" maxlength="10"  value="<?php if (isset($_POST  ['tax])) echo $_POST['tax'];  ?>" /></p> 

4.

Save the file as calculator.php, upload to your Web server, and test in your Web browser (Figures 3.10 and 3.11).

Figure 3.10. The form now recalls the previously submitted values…


Figure 3.11. …whether or not the form was completely filled out.


Tips

  • Because some PHP code in this example exists inside of the HTML form value attributes, error messages may not be obvious. If problems occur, check the HTML source of the page to see if PHP errors are printed within the value attributes.

  • You should always double-quote HTML attributes, particularly the value attribute of a form input. If you don't, multiword values like Elliott Smith will appear as just Elliott in the Web browser.

  • If Magic Quotes are on in your server, you'll need to apply stripslashes() before printing string variables as a form input's value:

     <input type="text" name="last_name"  size="20 value="<?php if (isset  ($_POST['last_name])) echo  stripslashes($_POST['last_name]);  ?>" /> 

  • On account of a limitation in how HTML works, you cannot preset the value of a password input type.

  • To preset the status of radio buttons or check boxes as checked, add the code checked="checked" to their input tag.

     <input type="checkbox"  name="interests value="Reading"  checked="checked /> <input type="radio" name="gender"  value="Female checked="checked" /> 

  • To preset the value of a textarea, place the value between the textarea tags:

     <textarea name="comments"  rows="10 cols="50"><?php echo  $_POST['comments]; ?></textarea> 

  • To preselect a pull-down menu, use selected="selected":

     <select name="year"> <option value="2005">2005</option> <option value="2006" selected=  "selected>2006</option> </select> 

    You'll see an example of this toward the end of the chapter.




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net