View Single Post
  #1 (permalink)  
Old Oct 6th, 2006, 17:08
masonbarge's Avatar
masonbarge masonbarge is offline
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Setting Form Values to Previously Entered Values

I'm sure a lot of developers know this, but still I invented it from scratch and thought I'd share it.

The problem is that, when you fill out a form and it is rejected for whatever reason, you don't want to make the user fill out the entire form again. You can use php to retain all the entered values when the form is rejected - usually because one box is not filled out, or doesn't meet the regular expression values, but also when the server is slow/down, whatever.

I've left out all validation (although afaik the only low-level danger in a POST method form is the text box, which in the example gets a simple 2 or 3 numeral validation. ).

The text box (or text) is easiest (I got this one from Ullman's book):

Code: Select all
Weight: &nbsp;</span>
            <input name="weight" type="text" class="text" value="
            <?php if (isset($_POST['weight'])) echo $_POST['weight']; ?>
            " size="3" maxlength="3"  /><br>
Here, if the value has been filled in, it will pass the "isset" test. Then you simply use "echo" to fill the same value back in.

Multiple choice form input is a little harder:

Code: Select all
            <div class="bmi_l">
                        <span class="b">                
Job (or Other Daily) Duties:</span><br>
<select name="job"class="text">

<option value="1" <?php if ((!isset($_POST['job'])) or ($_POST['job'] == "1"))
 echo  'selected="selected" '; ?>
>Desk Job </option>

<option value="1.03"<?php if ((isset($_POST['job'])) and ($_POST['job'] == "1.03"))
 echo  'selected="selected" '; ?>
>Move Around All Day (e.g. nurse, teacher, electrician)</option>

<option value="1.15"<?php if ((isset($_POST['job'])) and ($_POST['job'] == "1.15"))
 echo  'selected="selected" '; ?>
>Light (masseur, baker )</option>
Here we have a dropdown box. This will default to the first selection if there has not been an entry; if there has been a selection, it will re-enter it.

The means of presetting a select is: selected="selected". So here, to preset the value, we use a php conditional and echo: selected="selected" for the one we want to preset.

The first conditional is the default: if no value has been set (!isset) by the user, we echo "selected". We use an "or" in the conditional to add the case when the user has selected the first button; in either case, we want to preset the first value.

For all other values, we use the opposite. If a value has been set (isset), and the set value is the same as the selection's value, we preset the value to that selection. This actually works!

For radio buttons, we do the exact same thing, except we echo checked="checked". (Notice that strings and numerical values are treated exactly the same in all the examples.):

Code: Select all
Do you do regular weight training?</span><br>

            <input type="radio" name="muscle" value="1" class="text"
            <?php if ((!isset($_POST['muscle'])) or ($_POST['muscle'] == "1"))
             echo  'checked="checked" '; ?> >&nbsp;
   Occasionally or never.&nbsp;&nbsp;&nbsp;<br>

            <input type="radio" name="muscle" value="1.05"class="text"
            <?php if ((isset($_POST['muscle'])) and    ($_POST['muscle'] == "1.05"))
            echo  'checked="checked" '; ?>>&nbsp;
  Yes, as part of my exercise routine.<br />

            <input type="radio" name="muscle" value="1.2"class="text"
            <?php if ((isset($_POST['muscle'])) and    ($_POST['muscle'] == "1.2"))
            echo  'checked="checked" '; ?>>&nbsp;
  Yes and I have gained significant muscle bulk.<br />
I haven't done any checkboxes yet, and there might be an additional challenge with them, if someone wants to try. I'm about to leave for holiday. I'd start with the radio button form. If it doesn't work, my next idea would be to add a regular expression search.

If you're wondering about the strange form entries, this is from a BMI (Body Mass Index) calculator and Calories Required calculator I've been making. It's not fully styled or fully functional yet, but you can see it at
http://www.dhreport.com/articles/foodvalues/bmi.php

I don't know where it will be when I leave for the week, and I'm sure some of the function will be in midstream development, but some of it should be working. The entire "foodvalue" directory is just a day or two from completion.
Reply With Quote