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

Dylan Hobbs
9,279 PointsWhere does AJAX 'POST' data go?
I am trying to setup ajax-forms with polymer.
I know GET will pull data from the server, where POST will post data to the server.. but where does this data get posted to? If it's a user form, like email address, where is user data saved?
4 Answers

Ted Sumner
Courses Plus Student 17,967 PointsThis page discusses what I understand you want to do. Marcus is correct about json data.
http://stackoverflow.com/questions/21563495/storing-json-post-data-in-an-array-nodejs
I think storage in a database is a better practice in general.
What do you want to do with the data?

Marcus Parsons
15,719 PointsHey Dylan,
Both GET and POST can retrieve data from the server. When you use query strings in a URL, you can use GET to get those values in the URL. A lot of API's use GET to grab parameters for API calls, and they can be reconstructed very easily. It has its limitations (such as the character length of a URL) but is very useful.
POST, on the other hand, is data that is hardcoded into the body of the message sent to the server. POST data is only visible to the server, and once a hard refresh of a page occurs, POST data is destroyed. It cannot be retrieved unless it was stored into some container: a database, a local/session variable, etc. POST can send a virtually unlimited amount of data and can even be used to send binary files (images, text files, etc.); however, it can't be reconstructed without prior storage.

Dylan Hobbs
9,279 PointsHey Marcus,
Thanks for all the explanations. I do already actually know a lot of that info from the videos. GET is not what I would be interested in doing.
What I am trying to do for instance is take a users input from a field. Such as their 'name', 'email' they enter, or a value they select on a radio button for example. Either way I would like to send their answers or data to my server.
What I am needing help with is WHERE on the server does this data go? How can I view it after the page has been refreshed?
Thanks!

Marcus Parsons
15,719 PointsIf you're wanting to handle this in jQuery, you should definitely use the $.post() method: http://api.jquery.com/jquery.post/. I would still recommend using a back end language such as PHP to validate the data.
My suggestion is also to encapsulate your script used in the form in an anonymous function so that any data you have in the JavaScript isn't attached to the global window object (and easily modified):
!function () {
//javascript code for contact form in here
}();
You can check out my contact page for how I set up the JavaScript portion of my contact validation. http://www.marcusparsons.com/contactme.php

Ted Sumner
Courses Plus Student 17,967 PointsYou will learn about this in the PHP courses. There is an array that is accessed in PHP that has a variety of information in it. For example
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// the form has fields for name, email, and message
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
This code looks at the REQUEST_METHOD slot in the $_SERVER array for the submission method (in this case POST) and pulls data from the server $_POST array. The name, email, and message were posted by the user in a form. I do not know when the data is reset.
I believe that you have to send that data to a database for storage for later use.

Dylan Hobbs
9,279 PointsWhat if I just wanted the data placed into a json file on my server? is this possible/easy?
I just want the absolute most simple way to collect the data from a POST

Marcus Parsons
15,719 PointsEasy.
<?php
$json = json_encode($_POST);
?>
You can then access the POST data as JSON.

Dylan Hobbs
9,279 PointsOkay, great! Thanks for the help.
So just to recap, if I use jQuery/AJAX to 'POST' data, (such as a user's email) then that data will be sent through PHP and PHP will then post this data into a JSON Object?
How do I go about doing this, or which videos can I watch? I'm starting to feel overwhelmed.
I just want to collect user's email, name, etc.. that they enter on the page using AJAX.

Ted Sumner
Courses Plus Student 17,967 PointsI think you are putting the cart before the horse in your question. Your real question is "How do I save a user input email address for later use."
I think the answer depends on what you want to do with that email and when you want to do it. You may want to save it for later to build an email library for a newsletter. Or you may want to do something else with it like send them an immediate thank you email but not save it after that. Those different uses that would use different methods of storage and code.
PHP is a server side language. There is a course on Treehouse that I recommend. You will learn how to DRY your code and perform server action. For example, you can have a file that contains all the links for your nav bar. You add a page to your site and add it just to the one nav file. It updates on all the other pages on your site. You can also store all sorts of information for things like an e-commerce store.
In thinking more about your issue, the array used to store the e-commerce data in that project may work for your ultimate need, but, again, it depends on what you want to ultimately do with the email address.

Dylan Hobbs
9,279 Points@Theodore, Thanks! That's exactly what I am trying to do. Sorry for not wording it so well.
I would like to store the users email for later use. I have no problem where or how it is stored, as I will deal with that later. I just want to be able to see it in the future.
Which course video was it specifically that talks about storing the e commerce array?
Also, the web app I am building I am using polymer to build. Will polymer have any issues with PHP in comparison to Node?
Thanks! Just looking for the most simple way to store data for later with Ajax

Ted Sumner
Courses Plus Student 17,967 PointsI had never heard of polymer until your question, so I don't know for sure, but I don't see a reason why it would not work with PHP.
I would use this track to take the PHP courses in the correct order from the beginning. I am not sure I would have been able to jump to arrays without the background and understand it.

Dylan Hobbs
9,279 PointsDo you know which PHP course in that track it is? I do know quite a lot of the fundamentals of PHP. I'm sure i'd be able to jump in if I knew where to start.
Cheers

Ted Sumner
Courses Plus Student 17,967 PointsBuild a simple PHP application, Listing inventory actions.
I would also review the contact form if you ever intend to use php for contact forms. It covers some security issues that are important.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 Points+1 Theodore