Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Liban Shire
PLUS
Liban Shire
Courses Plus Student 4,147 Points

Adding a contact form - PHP

Building a simple PHP Application has a section on a contact form. I'm at the end but I notice one thing. I can still submit the forum even without entering any values in it? it takes to me the thank you section. Is this normal for now are we gonna learn a way to handle no input in the forum or more advanced things where you require a email for the email section only and stuff like that.

5 Answers

Cesar Guerrero
Cesar Guerrero
11,190 Points

There are ways to check if the user has not entered any information on either the client-side and or server-side. In this case, if you have no code checking whether or not any information was entered in or not so yes this is normal.

On the client-side, to check whether or not information was entreated in our not you can add the "required" attribute in your input types and the browser will check for you if the person has entered information or not and will even notify the user. This is largely supported in many browsers.

On the server-side, you can check whether or not the input values have anything by using the "empty" function along with if statement too decide what you would like to do with the user if they submitted the form without any data, like reloading the page or something along those lines.

You would have to implement some JavaScript.

Colin Marshall
Colin Marshall
32,861 Points

While this is certainly an option, wouldn't just using jquery be a "better" option for a PHP site? Adding AngularJS just for a contact form on a PHP-based site seems like a bulkier option with a higher learning curve. I have yet to experiment with AngularJS though, so I might be wrong. Would love to hear more from you on this.

Seems simple enough and probably worth learning: Easy Form Validation With AngularJS.

Liban Shire
Liban Shire
Courses Plus Student 4,147 Points

Isn't there a way to do this using PHP only? why mix JS in to a small codebase.

The only difference with JS is the user gets instant feedback vs an error message / page refresh with PHP and this makes for a better user experience. The other downside would be if the client has JS disabled (rare) so that would make PHP a more guaranteed solution. But to answer your question you most certainly can.

Liban Shire
PLUS
Liban Shire
Courses Plus Student 4,147 Points

Does the PHP Application course teach that or do we leave it at that?

Cesar Guerrero
Cesar Guerrero
11,190 Points

I'm not very sure, I would assume so. That is just some knowledge I've learned just by googling questions when I was learning basic PHP and HTML.

Liban Shire
PLUS
Liban Shire
Courses Plus Student 4,147 Points

My main question is does the course go over how to fix the problem or not?