Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery Ajax function not working

Posted on 2011-02-13
3
Medium Priority
?
362 Views
Last Modified: 2013-11-19
I am trying to use this code, a variation of which I have used successfully in the past, and I can't get it to work. I assume there must be some syntax or logic error that I can't find. Perhaps your eyes can discover it.

<script type=\"text/javascript\">

$(function(){
      var vehicle = $("#vehicle");
      var pickup = $("#PICKUP");
      var error = $("#error");
      var postwrap = $("#postwrap");
      $("#vsearch").submit(function(){
            if((pickup.val() == "") || (dropoff.val() == "")){
                  error.text("The post must have both a pickup and a dropoff location").fadeIn(300).delay(900).fadeOut(300);
                  return false;
                  }
            else{
                  $.ajax({
                        type: "POST",
                        url: "AJAX_process.php",
                        data: "vehicle="+vehicle.val()+"&pickup="+pickup.val()+"&add=true",
      success:
      function(r){
      postwrap.html(r);
      postwrap.hide().show(1000);$("#success").text("Post added").fadeIn(300).delay(900).fadeOut(300),
},
      error: function(){$("#error").text("Could not add post").fadeIn(300).delay(900).fadeOut(300)}
                  });
                  return false;
                  }
      }
)});
</script>

The form has the id of vsearch.

With this installed, and the form statement as simple as <form id="vsearch"> (no action statement), the form still refreshes and does not show the result in the divs

I have jQuery running OK on the page, powering some other functions. I am using it in som PHP code.

Any ideas?
0
Comment
Question by:birwin
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 2000 total points
ID: 34882153
Couple of things I found:

1. Following line should end with a semicolon, not a comma
postwrap.hide().show(1000);$("#success").text("Post added").fadeIn(300).delay(900).fadeOut(300),

Open in new window


2. In the following line you are using variable/object dropoff, but I don't see it defined or initialized.
 if((pickup.val() == "") || (dropoff.val() == "")){

Open in new window

0
 
LVL 6

Author Closing Comment

by:birwin
ID: 34882332
Thank you for pointing out my rather obvious errors. I had been staring at it for too long. I am quite sure I tried the code with a comma and a semi-colon, but you caught the problem with the undeclared variable, that I completely missed.

Thank you!
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34882749
Glad to help. Thanks for the points.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question