Link to home
Start Free TrialLog in
Avatar of birwin
birwinFlag for Canada

asked on

jQuery Ajax function not working

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?
ASKER CERTIFIED SOLUTION
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of birwin

ASKER

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!
Glad to help. Thanks for the points.