Okay... I messed around with things a bit and moved the php stuff to the top as you suggested. I have part of the validation working however if more than 1 error exists, it still only prints the 1st one.
Below are 2 example places where there would be an error... if I leave them both blank, they should both give an error message. $error=array(); if (strlen($username)< 3) { $error['username']="Username must be more than 3 characters"; } elseif (strlen($password)< 3) { $error['password']="Password must be more than 3 characters"; } <input type="text" name="username" value="<?=$username;?>"> <? if ($error['username']) echo "<br>".$error['username'];?> <input type="text" name="password" value="<?=$password;?>"> <? if ($error['password']) echo "<br>".$error['password'];?> Am I assigning errors to the array incorrectly? Thanks for your help :) Jason > -----Original Message----- > From: Jason G. [mailto:[EMAIL PROTECTED]] > Sent: February 18, 2002 9:19 AM > To: Matt; [EMAIL PROTECTED]; [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED] > Subject: Re: [PHP] form submission error trapping > > > Why do not you all just put all your PHP logic, db access, etc at the TOP > of the script. Once you have your results and variables created, then > start into html. > > This method produces MUCH cleaner scripts, and there is a very minimal > amount of PHP interspersed within the HTML. Also, you have time to bail > out, or redirect, etc... > > ------------------------------------------------------------ > <? > Connect to DB > > Are we to process the form? > Yes: > Validate fields > Generate Error Messages > break; > No Error Messages? > Update the DB > Header("Location: bla.php?NEWID=$NEWID"); > exit; > > ?> > <html> > > Firstname: <? if(error) echo(error message); ?><br> > <input type="text" name="txtFIRSTNAME" value="<?= > htmlspecialchars($txtFIRSTNAME); ?>"><br> > <br> > Lastname: <? if(error) echo(error message); ?><br> > <input type="text" name="txtLASTNAME" value="<?= > htmlspecialchars($txtLASTNAME); ?>"><br> > <br> > > </html> > > > > > At 08:35 AM 2/18/2002 -0500, Matt wrote: > >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 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php