Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Forms in PHP

Is it better to validate PHP forms using php and then redirecting with errors, or is it better to use javascript validation and then just allow form submission if javascript is enabled.

like image 933
Vish Avatar asked Dec 02 '22 02:12

Vish


2 Answers

You must validate values on the server side, since the client cannot be trusted.

You may validate values on the client side to provide better UX for those with JavaScript enabled.

like image 148
Matt Ball Avatar answered Dec 21 '22 03:12

Matt Ball


I usually do both. If you can check the fields in javascript, it saves them the loading time and spares your server the hit. You still have to check again on the server in case they had javascript disabled, or intentionally bypassed it.

like image 23
Tesserex Avatar answered Dec 21 '22 01:12

Tesserex