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 trial

HTML HTML Forms Form Basics The Form Element

Timothy Bramlett
Timothy Bramlett
6,609 Points

Nick says we need to use a server side language...

...to process the data input by the user on a form. Could we not also just use JavaScript to process the data?

I mean you couldn't really store any data long term since it is all running on the client side, but for simple web apps you could just use JavaScript, right?

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

First, Javascript won't send emails. You'll need a server side language to do that. You can get the values using Javascript, and Ajax them in, but you still need a server side language to do something with the values. If you're not planning on sending or storing the user form data, what is the form for?

If you just want to get the values of some input, and not send it, you just use on of Javascripts methods for getting values (or jQuery if you decide to include that library). You don't use a form, thus you don't have an action or method.

To have an action and method on a form, you need a server side language to handle that.

If you don't want to screw around with server side languages and doing something with that form data, you can easily include something like this: http://formspree.io

The only problem I see with this library is that your email is going to be in the source code, and might get you hit with spammers.

Truth be told, it's pretty easy to send emails with the PHP mailer website, and almost all hosting servers, even your most basic shared plans have PHP installed on them for you.

Timothy Bramlett
Timothy Bramlett
6,609 Points

That is good to know that JavaScript on the client side can't send emails butI won't need to send any though with this first app. These forms/fields will just be used to get input from the user and then JavaScript will display the results. Like a calculator.

Once I get into building apps where the data needs to be retained, I think I will go through a Flask book I have since I already have some exposure to Python. I have also heard you can use JavaScript server side with node.js so I may look into that.

Kevin Korte
Kevin Korte
28,149 Points

Ahh, now it makes sense.

Yes, you don't need a form if you just want to gather user input, do some sort of calculation or manipulation, and show it back to the user. Just get the values from the inputs and move on your way. As mentioned, you can use Localstorage in web browsers that support it to make the data persist when the user returns, but Localstorage is also vulnerable to deletion by the user.

It sounds like you're well on your way, and you have a good understanding here. And yes, I do not know Node very well, but I agree that I do believe you can send emails with Node. The whole Javascript server side thing is so cool, I just haven't learned it yet.

Good luck and happy coding!

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

What he means by 'process the data' here, is to have the info sent to a database, then stored or updated, and then have a response sent back and have a message shown (or not) to the user.

You could do 'fake' processing of the data, using the values input by the user to generate a fake response, but I think that's beyond the scope of the lesson.

Timothy Bramlett
Timothy Bramlett
6,609 Points

Got it, I think. I am going to mess around with it this weekend, but for some of the very basic web apps I want to create, at first, I really don't need to send or store any data in a database.

I am really kind of curious as to what I would put for 'action' and 'method', if I am using javascript to 'select' the values that the user has input into a form to throw them into variables and run them through a model. Maybe I leave them blank?

Robert Komaromi
Robert Komaromi
11,927 Points

If you don't care about users who may not have JavaScript enabled in their browsers, then you don't even need HTML forms. You can simply have input elements and use JavaScript to read those values. However, it still makes sense to use a form for semantic reasons.

Also, you can store data long term on the client side using the local storage API or cookies.

Timothy Bramlett
Timothy Bramlett
6,609 Points

@Robert That is good to know as I didn't know that was possible!