[PHP] Re: How to deal with errors in forms
Documented research indicate that on Fri, 25 Aug 2006 13:18:25 +0200, "Ivo F.A.C. Fokkema" wrote: > > You might also try to process the results from the form first, and then, > if errors are found, display the form again and put the data in there > yourself. No need to send the user back and forth. But you may need to > restructure your code a little. I personally always use this method. > > 1) Check if form is sent. > 1a) True? Check form contents. Put errors in a variable. If there are no > errors, do whatever you need to do. > 1b) False? Set all form fields to the default values (probably empty strings). > > 2) Check if error variable exists. > 2a) True? Print error variable on the screen. > > 3) Print form, and load values in them. The above method is basically what I use with great success. I've simply added my own alerthandler functions, meaning the form checking part sets a variable called $alert_level to a value between 0 and 3, 0 = no errors, 1 = notice, 2 = warning, and 3 = error, and then the handler itself checks what kind of alert to put out and displays a box coloured to match the alert level. The main reason for doing it like that is because I wanted something simple that I could reuse across all my pages, so alert messages look similar - people pay more attention to them if all your pages use the same method of notifying them of problems and errors. The alert_handler uses a second variable, $alert_message, that the form checker uses to tell exactly where the problem is, to avoid one of the "There was an error in your input" situations. On top of that, I'd like to suggest (or actually recommend) using POST instead of GET, especially when you use 2000 char fields. PHP doesn't care either way, but if you use POST you won't have the problem of field contents being cut off because they won't fit in the URL. And you can keep all your variable names the same, or even make them longer and easier to remember perhaps. Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to deal with errors in forms
On Thu, 24 Aug 2006 11:40:17 +0200, Merlin wrote: > Hi there, > > I do have a form where there is also a field with max 2000 characters > the user can put in. > > Now before processing the data with php, I do a checkin the script for > certain criterias if something lookes wrong I do redirect him to the > original form with inserting the data he has entered. I do this via GET > e.g.: ?title=test&body=blablub > > That works fine with one exception. If the user does enter 2000 > characters (or a lot c.) they do get transfered via URL as well and that > is not possible. Firfox for example then simply displays a blank page!!! > It would be fine if he would return with just a few less characters, but > at least display the error message I am providing. > > Now, 2 questions: > 1) Does anybody know why firefox is shoing a blank page? If the URL does > contain less characters, lets say 100 everything works fine. > 2) How could I possibly save his entry? Maybe with the help of a cookie? > But then, I do redirect to the page. So I do send a header. As far as I > know this only once possible? > For example: > setcookie('bla test'); > HEADER("Location:".$data[rurl]."?error=".$error.$parameter); > > I would rather not like to use the help of a database. > > Any ideas? > > Regards, > > Merlin You might also try to process the results from the form first, and then, if errors are found, display the form again and put the data in there yourself. No need to send the user back and forth. But you may need to restructure your code a little. I personally always use this method. 1) Check if form is sent. 1a) True? Check form contents. Put errors in a variable. If there are no errors, do whatever you need to do. 1b) False? Set all form fields to the default values (probably empty strings). 2) Check if error variable exists. 2a) True? Print error variable on the screen. 3) Print form, and load values in them. HTH -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to deal with errors in forms
Hi Merlin, try with sessions. in this case you don't have to append something to the URL as parameters Merlin schrieb am 24.08.2006 11:40: > Hi there, > > I do have a form where there is also a field with max 2000 characters > the user can put in. > > Now before processing the data with php, I do a checkin the script for > certain criterias if something lookes wrong I do redirect him to the > original form with inserting the data he has entered. I do this via GET > e.g.: ?title=test&body=blablub > > That works fine with one exception. If the user does enter 2000 > characters (or a lot c.) they do get transfered via URL as well and that > is not possible. Firfox for example then simply displays a blank page!!! > It would be fine if he would return with just a few less characters, but > at least display the error message I am providing. > > Now, 2 questions: > 1) Does anybody know why firefox is shoing a blank page? If the URL does > contain less characters, lets say 100 everything works fine. > 2) How could I possibly save his entry? Maybe with the help of a cookie? > But then, I do redirect to the page. So I do send a header. As far as I > know this only once possible? > For example: > setcookie('bla test'); > HEADER("Location:".$data[rurl]."?error=".$error.$parameter); > > I would rather not like to use the help of a database. > > Any ideas? > > Regards, > > Merlin > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php