DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   User Form error checking (http://www.daniweb.com/forums/thread71432.html)

nathanpacker Mar 2nd, 2007 9:12 am
User Form error checking
 
Hey everyone, I need some advice. I have a simple user form. Name, email, comments, blah blah blah.

Currently for error checking, in my code, if the user inputs something invalid or doesn't fill out a required field, I redirect them to another page that tells them the error, then they have to hit the back button to go back and fix it.

I'd like to make this process a little easier for the user. I've seen forms where if you miss a required field or something, it simply brings the form back up and at the top in red letters will tell you to fill in all the required fields or something, and then have like a red star next to those fields.

I'm sure if I sat around for a few hours I could come up with a real clunky, code-heavy way of doing this, but I was wondering if you all can give me some ideas. An easy way of doing this? I have a moderate understanding of php, so I know most of the basic functions and things. And when I come across one I don't know, I'll learn it, so bring it on.
Ideas?

stymiee Mar 2nd, 2007 10:57 am
Re: User Form error checking
 
No need to send them to another page. Data validation is easy to do.

1) Receive the POSTed form data (top of the file)
2) Validate it. Put errors into an array so you can echo them out later
3) Check to see if you found any errors. If not, do whatever you want to do with the data.
4) If there are error, redisplay the page and list the errors so the users knows what they did wrong.

This method also allows you to repopulate the form fields if there is an error so they don't have to type it in again.

nathanpacker Mar 2nd, 2007 11:30 am
Re: User Form error checking
 
Quote:

Originally Posted by stymiee (Post 323168)
No need to send them to another page. Data validation is easy to do.

1) Receive the POSTed form data (top of the file)
2) Validate it. Put errors into an array so you can echo them out later
3) Check to see if you found any errors. If not, do whatever you want to do with the data.
4) If there are error, redisplay the page and list the errors so the users knows what they did wrong.

This method also allows you to repopulate the form fields if there is an error so they don't have to type it in again.

Thanks for that. I think I get what you're going at. And I think I was able to Google something that also might help, right along the same lines of what you're saying. I'll let you know how it goes.
Thanks.

WhiteLeo Mar 3rd, 2007 12:35 am
Re: User Form error checking
 
Another idea is to also include a javascript validation. There are plenty of tutorials found through google. Just look up "Javascript Form Validation".

That way when the button is clicked, the form is instantly checked and a message displayed at that moment without a page having to be reloaded.

However, you still should included some server side validation, using php. The javascript is jut to make it easier for the person. The php is there incase the person gets around the javascript.

nathanpacker Mar 3rd, 2007 1:11 am
Re: User Form error checking
 
Quote:

Originally Posted by WhiteLeo (Post 323411)
Another idea is to also include a javascript validation. There are plenty of tutorials found through google. Just look up "Javascript Form Validation".

That way when the button is clicked, the form is instantly checked and a message displayed at that moment without a page having to be reloaded.

However, you still should included some server side validation, using php. The javascript is jut to make it easier for the person. The php is there incase the person gets around the javascript.

Very good idea! I'll take a look into that as well.

csscart Mar 4th, 2007 8:27 pm
Re: User Form error checking
 
Quote:

Originally Posted by stymiee (Post 323168)
No need to send them to another page. Data validation is easy to do.

1) Receive the POSTed form data (top of the file)
2) Validate it. Put errors into an array so you can echo them out later
3) Check to see if you found any errors. If not, do whatever you want to do with the data.
4) If there are error, redisplay the page and list the errors so the users knows what they did wrong.

This method also allows you to repopulate the form fields if there is an error so they don't have to type it in again.

Don't forget to sanitize and validate the data. You didn't say if you are storing in DB or emailing, but the PHP mail() function can be used by spammers if you're not careful with your data.

http://www.securephpwiki.com/index.php/Email_Injection

stymiee Mar 4th, 2007 10:06 pm
Re: User Form error checking
 
I guess you missed item number 2? ;)

Quote:

2) Validate it. Put errors into an array so you can echo them out later

nathanpacker Mar 5th, 2007 1:17 am
Re: User Form error checking
 
Quote:

Originally Posted by csscart (Post 324806)
Don't forget to sanitize and validate the data. You didn't say if you are storing in DB or emailing, but the PHP mail() function can be used by spammers if you're not careful with your data.

http://www.securephpwiki.com/index.php/Email_Injection

I'm storing the info in a mysql database, but also sending a copy of it to my email address. However, that should be hard for spammers to fake, since the user needs a special code verified before they can submit the form. Basically, the user puts in the code, which lets them to the main form. Then when they actually submit the form, the script again makes sure that the code is valid, then processes the info.

And FYI, your help has worked. I'm going it the way you said, submitting the form to itself, and I have a variable that is stored, that is at first set to nothing. Any errors that pop up append themselves to the variable. Then if there are errors, it shows the form with that variable at the top. Working splendidly.
Thanks!

Rhyan Mar 6th, 2007 12:27 pm
Re: User Form error checking
 
Nathan - just an idea - you could display the error messages next to the wrong fields, while the correct fields data is automatically entered into the correct field, so the user does not have to fill in all the fields. Or, even better - you can show only the wrong fields, that have invalid data, instead of showing all, and still error messages to be just beside the erroneous field.


All times are GMT -4. The time now is 2:46 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC