Re: values in forms
| From: | Matthew Leingang | Date: | Thu, 28 Sep 2000 19:03:58 +0000 |
| Subject: | Re: values in forms | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-17953@lists.php.net to get a copy of this message | ||
Bryan,
The reason this isn't working is that your setting of the variables in
processesform.php3 ends with that script. Once you redirect to
errorform.php3, you restore the values from variables.php3. $oldfields
may have some values in it, which is why $i is set, but not the ones you
set in the processesform script.
One way to fix this is to have processesform.php3 draw the new form and
dispense with errorform. However, IMHO it's best to do away with
processesform as well and have one script handle the whole process. That
way, you only have to write the code that draws the form once, and in the
case of an error in input, you can even handle the higlighting of bad
entries.
Here's an outline of the code:
if (form was submitted) {
// validate input
if (valid) {
//process input
}
else // form input not valid or not submitted
{
// draw the form
}
In the validation subroutine, you can populate an array $field_error,
indexed by field name or field number, that would tell the drawing
subroutine whether to put an asterisk or not. You can also set a flag
that gives a general error message. Such as
// inside validation sub ...
if (!ereg("[0-9]{5}",$zip) {
$form_error = 1;
$field_class['zip'] = 'error';
}
// ...
// inside drawing sub...
if ($form_error)
print "<p>There was an error in processing your form.
Please check the fields marked with an asterisk.</p>";
// ...
print ($field_class['zip'] == 'error' ? "*" : "") .
"ZIP code: ";
print "<input type='text' name='zip' value='$zip'>";
// ...
And, I hate to sound like a broken record, but PHPLIB (phplib.netuse.de)
has a class called tpl_form that implements this structure very well.
HTH,
Matt
----------------------------------------------------------------
Matthew Leingang http://www.math.rutgers.edu/
Rutgers University leingang@math.rutgers.edu
Department of Mathematics "This signature needs no quote."
On Thu, 28 Sep 2000, bryan brown wrote:
> I have a page with several input forms. If the fields
> are not set or are set incorrectly, a similar (but
> different) page will open with the same fields, but
> displaying an error message and an asterisk next to
> the first incorrect field. Is there a way I can keep
> all the values the way the user entered them before
> going to the error page? I tried making an array and
> when it checks the fields, it stores all the values in
> the array. And the error page prints the values in
> the fields, but it doesnt work. Its looks like this:
>
> <!-- processesform.php3 -->
> ...
> include('variables.php3');
>
> // $oldfields is in variables.php3
>
> $oldfields[0] = $f_name;
> $oldfields[1] = $l_name;
> $oldfields[2] = $email;
> $oldfields[3] = $organization;
> $oldfields[4] = $street;
> $oldfields[5] = $city;
> $oldfields[6] = $state;
> $oldfields[7] = $zip;
> $oldfields[8] = $country;
> $oldfields[9] = $h_phone;
> $oldfields[10] = $b_phone;
>
> // Check if all fields are in correct form.
> // If they are not, go to errorform.php3
> ...
>
> <!-- errorform.php3 -->
>
> ...
>
> include('variables.php3');
> ...
>
> echo "<td><input type=\"text\" value=$oldfields[$i]
> name='$value'></td></tr>\n";
>
> $i is set, but the fields still remain empty.
> Any help????
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - 35mm Quality Prints, Now Get 15 Free!
> http://photos.yahoo.com/
>
> [ Part 64: "Included Message" ]
>