[nycphp-talk] Managing form data with PHP
David Krings
ramons at gmx.net
Tue Dec 11 14:02:14 EST 2007
Tony Furnivall wrote:
> The 'pretty' enhancements are relatively easy - but I'm having a problem
> trying to get the form data to redisplay.
>
> Any pointers? ideas? suggestions?
I am sure there are better ways, but this is what I do:
I submit the script to itself and check with isset() which values in $_POST
are set. With a good initialization of variables you get a very reliable means
of checking if there was data submitted at all (after getting values from
$_POST unsubmitted variables retain their initialization value) and when it
was submitted (variable valuye is no longer the initialization value) one can
check for plausibility, such as checking if a date entered really is a valid
date (PHP has nice functions for that). Based on the outcome of all this
checking I set the default value of the form elements to the submitted and
accepted value or show an appropriate error message (which you say you have
handled with the 'pretty' stuff). Upon a submit that generates no error I
write the values to $_SESSION and redirect the browser to the next script, for
example one that writes the values to the database. Of course, you can also
show a message that all entries are OK and place a button on the page. Upon
clicking that button the whole entires are submitted via hidden fields through
$_POST to a different script.
Or you could just stuff all the code into one big script and redirect to
itself with commands sent along through the URL parameter that can be
harvested from $_GET. You ideally create a huge switch statement that acts
upon that activity tag. I like many script better than one big one, but I can
see advantages for both approaches. There are some HTML form fields where you
do not want to (or even cannot) set default values from previous submissions,
such as password fields. It is common practice that those entries are not
preserved. There are two ways to get the previously submitted value to show in
drop-down lists as current selection. The easy, cheesy method is to make sure
that it is the first entry in the list, which is the default selection if not
specified otherwise. You will have that option then in the list twice. The
nicer approach is to set the selected flag appropriately. Likewise for radio
button groups and check boxes.
I use this in some cases, but do so sparingly. If the form contains three
fields I don't bother. It is nice to do this for large forms. It can get
confusing at times and there is a lot of checking needed so that you don't get
screwy results for the various states of the page/form. But, I found it to
work well and in a way that I can comprehend and troubleshoot. Add a lot of
comments so that in a week from now you still have a clue as to why there are
all these checks in there. The script will get quite complex compared to one
that 'only' shows a static form.
Hope that helps,
David
More information about the talk
mailing list