For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > September 2006 > Re-populating form after submission









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re-populating form after submission
JWL

2006-09-27, 7:57 am

Hi

I have a large-ish form (about 100 fields) that must be verified on the
server. My question is, what is the best way to re-populate the form
with the visitor's data in the event that the form is rejected?

I know that the data will be in $_POST and could easily be re-inserted
into the form... However, it feels kind of messy to do a check for every
single field, in order to know whether to insert the existing value from
$_POST.

Another option I can think of is divide the page like this:

if (form was submitted correctly)
display thank you message
else if (form was submitted with errors)
display form with $_POST data, so visitor can correct errors
else if (form was not submitted)
display blank form

It seems odd though, for such a large form, to have two copies of the
form on the page.

What do you normally do in this situation?

Thanks for looking
Ron Barnett

2006-09-27, 6:58 pm

"JWL" <user@example.net> wrote in message
news:4nvenoFcb2enU1@individual.net...
> Hi
>
> I have a large-ish form (about 100 fields) that must be verified on the
> server. My question is, what is the best way to re-populate the form with
> the visitor's data in the event that the form is rejected?
>
> I know that the data will be in $_POST and could easily be re-inserted
> into the form... However, it feels kind of messy to do a check for every
> single field, in order to know whether to insert the existing value from
> $_POST.
>
> Another option I can think of is divide the page like this:
>
> if (form was submitted correctly)
> display thank you message
> else if (form was submitted with errors)
> display form with $_POST data, so visitor can correct errors
> else if (form was not submitted)
> display blank form
>
> It seems odd though, for such a large form, to have two copies of the form
> on the page.
>
> What do you normally do in this situation?
>
> Thanks for looking


Hi,

I would (on a validation failure) copy the relevant $_POST data fields
required to re-populate the form to variables.

then the body of the form would look like
( in raw HTML)
<input type='text' name='f1' value ='<?PHP echo @$f1; ?>' >
....
....
in PHP it would be
echo "<input type='text' name='f1' value ='@$f1' >";
....
....

If the variable $f1 has a value (validation error condition) the value will
be pre-loaded into the form.
If the variable is unassigned (blank form) the prefix @ will suppress an
error and continue.

You could of course preload all the variables to '' and omit the @ but that
seems to be a longer way round to me.

HTH

Ron



usenet@isotopeREEMOOVEmedia.com

2006-09-27, 6:58 pm

On Wed, 27 Sep 2006 15:04:38 +0100, JWL <user@example.net> wrote:

>Hi
>
>I have a large-ish form (about 100 fields) that must be verified on the
>server. My question is, what is the best way to re-populate the form
>with the visitor's data in the event that the form is rejected?
>
>I know that the data will be in $_POST and could easily be re-inserted
>into the form... However, it feels kind of messy to do a check for every
>single field, in order to know whether to insert the existing value from
>$_POST.
>
>Another option I can think of is divide the page like this:
>
>if (form was submitted correctly)
> display thank you message
>else if (form was submitted with errors)
> display form with $_POST data, so visitor can correct errors
>else if (form was not submitted)
> display blank form
>
>It seems odd though, for such a large form, to have two copies of the
>form on the page.
>
>What do you normally do in this situation?
>
>Thanks for looking


For text fields, you could simply echo the POSTed value by default (after
sanitizing it earlier in the script). If there was no value submitted, it's
just empty.

<?php
$sanitized_field1 = mysql_real_escape_string($_POST['field1'
]);
// or whatever is required in your case
?>

<form>
<input name="field1" id="field1" type="text" value="<?php echo
$sanitized_field1; ?>" />
</form>

I'd be interested if anyone sees a security (or other) flaw in this approach.
Jussist

2006-09-28, 6:56 pm

One possibility is to use some sort of qvasi-framework, inna spirit of.

[FORM.php]
$fields = array("firstname", "lastname", "someotherfield");

for($i=0;$i<count($fields);$i++) {
showField($fields[$i]);
}

function showField($field) {
$val = "";
if(isset($_POST[$field])) {
$val = $_POST[$field];
}
echo '<input type="text" name="'.$field."' value="'.$val.'">';
}

And for the action-page, just do the same loop. If the fields are
complex, then, well, refine the functions and idea.

JWL wrote:
> Hi
>
> I have a large-ish form (about 100 fields) that must be verified on the
> server. My question is, what is the best way to re-populate the form
> with the visitor's data in the event that the form is rejected?
>
> I know that the data will be in $_POST and could easily be re-inserted
> into the form... However, it feels kind of messy to do a check for every
> single field, in order to know whether to insert the existing value from
> $_POST.
>
> Another option I can think of is divide the page like this:
>
> if (form was submitted correctly)
> display thank you message
> else if (form was submitted with errors)
> display form with $_POST data, so visitor can correct errors
> else if (form was not submitted)
> display blank form
>
> It seems odd though, for such a large form, to have two copies of the
> form on the page.
>
> What do you normally do in this situation?
>
> Thanks for looking


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com