943,712 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6488
  • PHP RSS
Mar 2nd, 2007
0

User Form error checking

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 2nd, 2007
0

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.
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Mar 2nd, 2007
0

Re: User Form error checking

Click to Expand / Collapse  Quote originally posted by stymiee ...
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.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 3rd, 2007
0

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
WhiteLeo is offline Offline
16 posts
since Feb 2007
Mar 3rd, 2007
0

Re: User Form error checking

Click to Expand / Collapse  Quote originally posted by WhiteLeo ...
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.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 4th, 2007
0

Re: User Form error checking

Click to Expand / Collapse  Quote originally posted by stymiee ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csscart is offline Offline
3 posts
since Mar 2007
Mar 4th, 2007
0

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
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Mar 5th, 2007
0

Re: User Form error checking

Click to Expand / Collapse  Quote originally posted by csscart ...
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!
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 6th, 2007
0

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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help. Error messaage. Can't upload pictures to grid
Next Thread in PHP Forum Timeline: PHP code has ISSUES-help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC