Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery Ajax function not working

Posted on 2011-02-13
3
Medium Priority
?
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

730 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