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?

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.

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.

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.

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.

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

I guess you missed item number 2? ;)

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

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!

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.

This article has been dead for over six months. Start a new discussion instead.