Search code examples
phpjqueryajaxformsuploader

How can get current id of form in AjaxForm method?


$('form').ajaxForm
({

beforeSend: function() 
{
// for example i need get current id form here how can ?
// codes
},
uploadProgress: function(event, position, total, percentComplete) 
{
// codes
},
success: function() 
{
// codes
},
complete: function(xhr) 
{
// codes
}


}); 

I used 2 forms by different id in my page.for example :

<form id="sample1"></form>

<form id="sample2"></form>

i need get current form id in $('form').ajaxForm . how can do that ?


Solution

  • With the help of @a.4j4vv1 answer, here's a complete solution:

    $(function(){
    
        var currentform; //declare variable here
    
        $("form[id^='sample']").ajaxForm ({ //"form[id^='sample']" means all forms with id starting with the word 'sample'
    
            beforeSubmit: function(formData, jqForm) {
                currentform = jqForm[0]; //retrieve form and assign variable here
            },
    
            beforeSend: function() {
                console.log("Ajax BeforeSend from "+currentform.id);
            },
    
            uploadProgress: function(event, position, total, percentComplete) {
                console.log("Ajax uploadProgress from "+currentform.id);
            },
    
            success: function() {
                console.log("Ajax success from "+currentform.id);
            },
    
            complete: function(xhr) {
                console.log("Ajax complete from "+currentform.id);
            }
        });
    });