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

JavaScript

Codepen Ajax probelm

$("#emailSignUpButton").click(function() {

   $.ajax({
     url: $("#SignUpForm").attr("action"), 
     type: "POST", 
     success: function(result){
               $("#emailSignUp").html(result);
              }}).fail(function() {
                alert( "Get failed!" );
                          });

Hi guys! Because of the huge amount of code my html has, i just copy this small piece of javascript code here. What i am trying to do is very simple: User put their contact information and then the ajax will return a message.

The basic idea of the html structure is something like this:

<div id = "emailSignUp"> <form id = "SignUpForm"> </form> </div>

I am just confused why it does not work? I have checked the javascript file link and the CDN but nothing seems to be wrong.

Can you link to the CodePen?

3 Answers

1) Add e.preventDefault() to the event handler of the #signUpForm just like you did for the submit button. Right now it seems to be refreshing the page which is preventing us from seeing other issues.

2) It looks like you're using the slim build of jQuery, which has $.ajax() left out. Here’s the CDN link for the full version:

<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

3) Your form doesn’t have an action attribute, it’s this: <form id = "SignUpForm">, so when you call $("#SignUpForm").attr("action”) it doesn’t find anything and you don’t actually hit any URL.

Thank you Brendan! Just one question.

When you talk about the action link, if i do not have a server or php file, can i link it to my local computer?

Hmm. Not for “POST” I don’t think. What are you trying to do?

I am thinking about localhosting the data since i do not have a server or PHP file for this simple application. Maybe after sending the data, i can get some return information such as the time i sent the data etc. But right now i do not know what to put inside the action attribute.

If the goal is for the user to be able to save their todos so they can look at them later, even if they've closed the browser window, you can use localStorage. You don't need to send any ajax request for that, it's all happening in the client's browser.

If the goal is to save information for all of your users in one centralized place, you're going to have to have some sort of server and database. There are some tools like Firebase that are free/cheap and a lot easier to get up and running than traditional setups.

Okay okay gotcha! Thank you overly much Brendan for your work!