php_guy Posted February 12, 2011 Share Posted February 12, 2011 Let's say I have a form with 5 fields to fill out Is it better practice to use Javascript to ensure that all fields are non-empty, a certain length, contain certain characters, etc. Or should the back end PHP side of things handle this, then return an error code to be printed out, etc Thanks! Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/ Share on other sites More sharing options...
KevinM1 Posted February 12, 2011 Share Posted February 12, 2011 You should always use PHP regardless. Why? Because JavaScript can be turned off in the browser. That said, you can use JavaScript on top of PHP, as an extra layer to add more responsiveness for the user. The typical way to do it is to run JavaScript validation first. If things check out, then the PHP script will take a hold of the form data and do its own validation on it. If the JavaScript validation finds something wrong, it can alert the user immediately without sending the data on to the back end. Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1173343 Share on other sites More sharing options...
php_guy Posted February 12, 2011 Author Share Posted February 12, 2011 You should always use PHP regardless. Why? Because JavaScript can be turned off in the browser. That said, you can use JavaScript on top of PHP, as an extra layer to add more responsiveness for the user. The typical way to do it is to run JavaScript validation first. If things check out, then the PHP script will take a hold of the form data and do its own validation on it. If the JavaScript validation finds something wrong, it can alert the user immediately without sending the data on to the back end. Ahhhh that makes sense, and sounds very thorough. So basically program your PHP as if there is no Javascript -- but optionally include the javascript anyway, as a means to reduce server hits for something as simple as a missing field (but don't rely solely on this) Is that sort of the rationale? Thanks Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1173346 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Nightslyr is right on the money. Javascript can't be considered validation at all. It isn't anything more than a convenience to the user (when properly done). Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1173351 Share on other sites More sharing options...
BlueSkyIS Posted February 12, 2011 Share Posted February 12, 2011 So basically program your PHP as if there is no Javascript -- but optionally include the javascript anyway, as a means to reduce server hits for something as simple as a missing field (but don't rely solely on this) Is that sort of the rationale? Not exactly. The point is primarily avoiding bogus data. It is very easy to turn off javascript and enter anything (or nothing) and submit a form. spam bots don't even use javascript, so they will send you as much email as you can handle without thinking about any browser-based validation. Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1173353 Share on other sites More sharing options...
beatricedns Posted April 27, 2011 Share Posted April 27, 2011 Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them. Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1206799 Share on other sites More sharing options...
Muddy_Funster Posted April 27, 2011 Share Posted April 27, 2011 Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them. Nightslyr is right on the money. Javascript can't be considered validation at all. It isn't anything more than a convenience to the user (when properly done). I'm thinking hotmail as a good example of when it's not Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1206801 Share on other sites More sharing options...
KevinM1 Posted April 27, 2011 Share Posted April 27, 2011 Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them. Don't resurrect dead threads. Link to comment https://forums.phpfreaks.com/topic/227477-javascript-vs-php-for-form-validation/#findComment-1206836 Share on other sites More sharing options...
Recommended Posts