Forum Moderators: coopster

Message Too Old, No Replies

Correct validation logic in forms

         

bagheera

12:40 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Hi,

I have a question which have bothered me for a while - thats to do the validation when a form is submittted:

How can validation be done in the best way? I have come on the alternatives:

1- making the validation in the php after submitting the form
--> then you handle in the server level, but would the data that the user enters still exist when you redirect to the same page (probably globals is needed then)
--> how would the error messages be handled - would you redirect to a totally new page or the same the user came from?

2 - Using Javascript.
--> This I like because you directly apply messages at the instance data is entered.
--> But this is Client side - so its not a REAL validation.

I like the usability of 2) and the consistency of 1)

But what is the best practices?

Im pretty new in PHP/Javascript and web development and do not want to start in a wron direction....

thanks

coopster

12:53 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your target audience is the best determining factor, but, bare minimum, you should do server-side editing. I often use both. Client-side javascript to edit form values so the user won't have to wait for server-side processing, but for sure do the server-side editing as you can't trust the editing to the client-side.

Yes, I display any form values that they had filled in prior to submittal and show them again on the display, along with any error message(s). Sometimes I will redirect to a new page, but that depends on the error message...maybe they are trying to access a page that they haven't authenticated themselves to yet, so I'll send them to a login page with a corresponding error message.

Birdman

1:11 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with coopster, do both! It's more work for you but it makes it easier for the user. I basically build my JavaScript error check and then emulate it in PHP on the target page.