0

I have an Ajax form. i am caputuring the ID of the form. and when i the ajax call is a success. i want to pass this Id form BeforeSubmit to success. so that i can append the results to a place where i want.

here is the code

function StatusComments() {

    $('.status-comment').submit(function() {
        $(this).ajaxSubmit(options);
        return false;
    });

    var options = {
        beforeSubmit: showRequest,
        success: showResponse,
        resetForm: true
    };

    function showRequest(formData, jqForm, options) {
  var formID = $(this).attr("id");

    }

    function showResponse(responseText, statusText, xhr, $form) {
  alert(responseText);
  var formID = $(this).attr("id");
  alert(formID);
    }

}

1 Answer 1

4

The form is passed as argument to the success handler:

var formID = $form.attr('id');

Also I notice that you are using the jquery form plugin and still subscribing to the .submit event of the form which is not necessary:

$(function() {
    var options = {
        beforeSubmit: showRequest,
        success: showResponse,
        resetForm: true
    };
    $('.status-comment').ajaxForm(options);
});

function showRequest(formData, jqForm, options) {

}

function showResponse(responseText, statusText, xhr, form) {
    var formID = form.attr('id');
    alert(formID);
}
Sign up to request clarification or add additional context in comments.

16 Comments

@Darin: i tried with the AjaxSubmit. didnt get it to work :( i have many .status-comment on the page. i want to pick up the one i clicked
thanks. when ever i use the ajaxForm alone. i am not able to trigger the success function. so i am using with submit. i dont get any error too
Well, there must something wrong with your code because that's the way it is supposed to be used. It's much easier.
ok. is it bcos. i am redenering the html at the form execution end ?
Yes, it's exactly because of this. When you call .ajaxForm, the form must be present in the DOM. So if it is a dynamically generated form you need to call the .ajaxForm after it is inserted in the DOM.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.