This is a discussion on "Setting Form Values to Previously Entered Values" within the PHP Forum section. This forum, and the thread "Setting Form Values to Previously Entered Values are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Setting Form Values to Previously Entered Values
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
||||
|
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):
Multiple choice form input is a little harder:
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.):
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. |
|
|
|
|||
|
Re: Setting Form Values to Previously Entered Values
The checkbox is a little tricker because when you check a checkbox what gets passed back is the value of 'value'.
When you want to reshow the form, you need to set the 'checked' parameter for the box to appear checked not the value parameter. Hopefully the code snippets below will make it clear.
|
|
|||
|
Re: Setting Form Values to Previously Entered Values
Yes, "sticky fields" (as I know them) are a vital part of a user-friendly application.
Ideally, you should go a little further and protect the echoed text against user inputs that include characters such as " ' \ and <. Try entering 6' (for six feet) or "33 onto the form who's URL you gave and you'll find the echos are rather odd. Would people really do such a thing? Absolutely they would - it's the basis of things like 'injection attacks' against a site! People will enter all sorts of things to try to find a hole in your security, and once they spot that you don't sticky your fields quite right, they'll take it as a clue that it's worth their while to hunt for strings that produce more sinister side effects that the ones I've suggested as examples for you to look at. With this additional logic, your code will get more complex - indeed, your select and checkbox code is already verging on the verbose. Put the common code into a function so that it's overall much shorter, so easier to maintain ... and that will also ensure that all you tests and fixes, once tested for one input element, should work consistently for all of the others of the same type. Some of this stuff isn't intuative. I've just completed the presentation of two PHP courses this week for the same company. The first group, fairly new to PHP, followed through the excercise that "bullet proof"s a sticky input box with a degree of doubt as to how vital it was for their particular applications. The second group, who have had rather more practise at PHP already, were delighted to apply the techniques I've mentioned in this post- "so that's how to keep the code manageable and robust" they said - a lightbulb moment. Last edited by grahame; Oct 7th, 2006 at 04:15. |
|
||||
|
Okies, how about this?
and the ternary form didn't seem to like an internal echo.Thx for the push, I needed to get comfortable with this on a real page. |
|
|||
|
Re: Setting Form Values to Previously Entered Values
I would tend to generalise further (and perhaps not bother with objects) ... to give you an idea, here's a (working, tested) example:
|
|
||||
|
Re: Setting Form Values to Previously Entered Values
Actually, the main reason I did a class was for self-education, i.e. my "assignment" was to create a class with a function and use it in a live page.
Your code made my eyes boggle. Two utterly different methods with the same result. Thanks for posting it -- I'm just starting with security issues, so the htmlspecialchars() usage was especially helpful. Question: Could you just use a regular expression restricting the input to one of the button values? |
![]() |
| Tags |
| default, form, php |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem setting the left/right/top/bottom padding of a table to different values | tonyb | Web Page Design | 2 | May 2nd, 2008 20:30 |
| Taking values from a form without sending it... | Mazinger | JavaScript Forum | 4 | Jan 8th, 2008 18:39 |
| form not updating values | djanim8 | Classic ASP | 0 | Dec 10th, 2005 19:05 |
| Multiple Choice Form Values | Andy K | PHP Forum | 9 | Aug 30th, 2005 12:01 |
| Assign string values to integer values of a session variable | Andy K | Classic ASP | 1 | Jul 13th, 2005 08:29 |