I think that mixing of html and php is too complex and leads to hard to
maintain scripts. I find it extremely difficult to understand a scripts
logic when it's spread out over hundreds of lines of php/html.

I use EasyTemplates that came in Web Applications Development with PHP 4.0
by Tobias Ratschiller and Till Gerkin. It looks like the source is
copyrighted and you have to buy the book to get it.  But it says it's based
on FastTemplates.  The book itself is at
http://www.amazon.com/exec/obidos/ASIN/0735709971/

You can see a sample of the method here
http://marc.theaimsgroup.com/?l=php-general&m=101371611609042&w=2

Notice that the form action handler is the same as the original form. This
is a simple example, but it does "remember" user input as you want.

I wrote a couple of helper functions that accept db names to build select
boxes and radio buttons.  They return strings of the html with the selected
value, and I just put them in a template container reserved for them (the
{TEMPLATE_ITEM} in the sample).

For tables with unknown number of rows, I have one template of the main
page, one for the table container, and one for each row itself.  I loop on
the row, using the row template and concatenate all of the rows of html into
a string.  I take that string and stick it into the table template, and
finally stick the table into the page template.  Very smooth, easy to read,
and no trouble with headers() since all output is done on the last
statement.  You have complete control over the script until then, and can
bail out to another page, or decide to use other templates and output
something else.

As for errors, I build an array of the messages such as:
if (!empty($thatsThere)) {
   $errors[] = "Don't put $thatsThere in  there";
}
if (empty($userName)) {
  $errors[] = "Username must be supplied";
}

Then you can tell if all is okay, with if (is_array($errors)).  If it is an
array, then something is wrong, so I  append the array contests into html
like this:
foreach($errors as $value) {
 $errorMsgs = $value . "<br>\n";
}
and then put $errorMsgs into the page template container you've reserved for
it.

----- Original Message -----
From: "Jason Dulberg" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>

> I am working on some error trapping for several forms on my site. After
> visiting a bunch of websites, I've noticed 2 common methods of displaying
> error messages.
>
> 1. display an error box on a new page and force the user to hit the <back>
> button
>
> 2. display the form again with appropriate error text and pre-filled
fields.
>
> I have part of the error on the new page working but I'm running into the
> infamous no contents in the form after going <back>.
>
> There are some useability issues with forcing the user to hit the back
> button -- some just don't want to bother.
>
> Is there a way to display the form w/original contents and error messages
> 'without' having to code the entire form twice? I have about 5 forms with
50
> fields or so each.
>
> What would be the best way to go about redrawing the form with the errors
> shown beside each field?
>
> Any suggestions are greatly appreciated.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to