0

As of jQuery 1.8, the .ajaxStart() method should only be attached to document.

I have two different forms on my page, both which I want to submit using ajax, and display a 'sending' while it's happening. I want #contact_response to display a message when the #contact_form is submitted like so:

$(document).ajaxStart(function() {
    $("#contact_response").html("Sending...");  
});

And similarly, #booking_response to display a message when #booking_form is submitted. However, doing this causes the other element to display Sending.... If you're only meant to attach ajaxStart() to the document, how are you meant to send multiple responses, but only update certain sections?

Is this the wrong function to use?

tgun926
  • 1,573
  • 4
  • 21
  • 37

2 Answers2

0

You could use:

$(document).ajaxSend(function (event, xhr, options) {
    if (options.url === "targetedScriptPath") $("#contact_response").html("Sending..."); 
}).ajaxComplete(function (event, xhr, options) {
    if (options.url === "targetedScriptPath") $("#contact_response").empty(); 
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

as far as i know the ajaxStart() and ajaxStop() functions are good if you want to trigger something globally on your page when some ajax-action is going on.
i ran over a similar problem, and that's how i solved it: just trigger the action with the event that submits your form (maybe some click-event) or add an event-listener for the form submission like for example:

$('#contact_form').on("submit", function() {
    $("#contact_response").html("Sending...");
});
$('#booking_form').on("submit", function() {
    $("#booking_response").html("Sending...");
});

to trigger the action after the ajax function has been completed, use the callback-function of whatever ajax function you are using:

$.post("booking_update.php", {
        //post vars
        post_var1: post_text
    }, 
    function(result) {
        //callback function
        $("#booking_response").html("Success!");
    }
);
low_rents
  • 4,481
  • 3
  • 27
  • 55