| | |
User Form error checking
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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?
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?
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.
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.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
•
•
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.
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.
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.
•
•
•
•
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.
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
http://www.securephpwiki.com/index.php/Email_Injection
I guess you missed item number 2? 

•
•
•
•
2) Validate it. Put errors into an array so you can echo them out later
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
•
•
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
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!
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.
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: How to Grab the data from other websites?
- Next Thread: PHP code has ISSUES-help me
| Thread Tools | Search this Thread |
# 5.2.10 action address ajax apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image include insert ip javascript joomla limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop paypal pdf php popup problem query radio random record recursion regex remote script search security server sessions sms soap sockets source space sql syntax system table tutorial update upload url validator variable video web xml youtube






