![]() |
JS Effect on error with PHP
HI all,
I am working on a form that I would like to be able to show errors on. Say for example they don't fill in the correct answer in one Input... when they click Submit, I run PHP to get $_Post and then if there is an error I catch it and tell them. Right now I tell them at the top of the page, however, I would like to highlight the inputbox or even the div where the error occered. In PHP do I catch all the problems and store them in vars then display them in a form rewritten? Can I use JS such as the Prototype library to SHAKE the incorrect answer area? Thanks for any advice or help! *!* |
No, what you want to do is use JavaScript and the DOM to evaluate that input before the form is submitted, possibly even as they are typing. If there are errors, use the JS DOM functions to manipulate elements on the page, show error messages, etc. Then if the form values are acceptable, allow the form to be submitted to the PHP script handling the input and check the values again. This ensures the user doesn't 'get around' your client side validation by disabling JavaScript. For those that do have JS enabled, their experience is much easier and doesn't require a page load just to check for valid input.
I don't know if 'shaking' the field is a great idea, I tend to avoid all the fancy animations these frameworks allow. Your web application should degrade gracefully and work identically for those users that have JS disabled, have an older or different browser, etc. So giving the user feedback on what's wrong with their input can be identical whether done via JS or PHP. |
Thanks for the reply. Since JS might not function on all browswers, shouldn't I just go about this with PHP errors and leave JS behind?
Thanks! |
I have to say, that doesn't make alot of sense. You're concerned that the user is either using an outdated browser or has JavaScript disabled, but you want to use Prototype to create some effects after the fact. It's somewhat of a catch-22.
If you have even a basic client side validation in place, it can catch most problems before submission. The odds that your user base is using updated browsers and have JavaScript enabled I'm sure greatly outweighs the opposite. The only caveat, as I mentioned previously, is to never rely on JS alone but have an even better validation method on the server. |
question regarding the server side validater.
lets say if js is disabled for a user and they go ahead and fill out the form and submit the page and i have a die or exit function that dies and displays them a message of what they mistyped or something. is there a better way to represent the data rather than showing them a blank white page with a die message? |
sarmenhb> You shouldn't kill the script and just show the user a blank page, your script should handle errors and bad data gracefully. If your script incorporates the user entry form, then add some logic that still shows the form but displays a relevant error that they can correct. If the script is separate and simply handles the input, use a header() call to redirect back to the form page, passing an error handling action in the URL query string.
|
| All times are GMT. The time now is 11:20 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0