Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJonathan Grieve
Treehouse Moderator 91,253 PointsForm processing
Is it possible to prevent a form from submitting if there are invalid fields entered using ONLY PHP or do you need to use another language like JavaScript?
I'm trying to understand how to properly handle form processing using more than just JS to empty form submissions.
3 Answers
Joel Bardsley
31,249 PointsA PHP-only method wouldn't prevent form submission/processing without either reloading the page or sending the user to another page. If you don't mind this, then you can absolutely do it in just PHP with form errors or a success message displayed when the page reloads.
If you wanted to improve the user experience without using JS, you could try the HTML5 built-in validation to prevent initial form submission.
On the above link you'll see examples of the required attribute to prevent empty fields, the pattern attribute for a more robust option matching a regular expression, and more. I don't need to tell you to still use PHP to process and validate the form data on the server, but I'll mention it for anyone else who comes across this post.
<noob />
17,062 PointsIn which language is easier to make backend apps phi or node js?
Jonathan Grieve
Treehouse Moderator 91,253 PointsI'm not familiar at all with node.js I'm afraid. I'm asking about server side form processing for websites with PHP. I have the tools I need to do validation with jQuery but I'm looking to expand my knowledge significantly on this. :-)
<noob />
17,062 Pointsif i’ll see the node js courses here it looks really powerful. and u can do real time apps
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Joel,
Thanks for that. I guess that answers my question. :-)
indeed I use HTML5 validation in my forms but it never feels like enough to just use that, and as I say in most cases I also use added jQuery validation which seems to be enough for my needs but again I just feel that PHP processing is needed to not only process validation but make things secure too.
So in conclusion, it's never enough to use just one method or the other. You need multiple layers of code to make good secure forms.
Joel Bardsley
31,249 PointsJoel Bardsley
31,249 PointsYou should only think of client-side validation (HTML5, JS, jQuery) as beneficial for the user's experience. Always assume someone will be able to bypass it and be able to submit malicious data, in which case you should do your main validation on the server.