Nob0dy
Nob0dy

Reputation: 1

Pass several variable to a function Jquery

Noob question incoming: Im using the jquery function loadNewPicture() to load picture (obviously) and the function progress() to see the achievement in percent of the upload.

Everything is working fine.

My question concerns only the variables that I can pass to the function "progress()." This function is a template that I took somewhere. It passes fine the variable "e" without specifiying it when using it by default:

myXhr.upload.addEventListener('progress', progress, false);

But I dont know how to pass more variables. I would like to pass the variable "category" so I was expecting something like this:

myXhr.upload.addEventListener('progress', progress(e, category), false);

But it does not work. Any idea?

function loadNewPicture(category, idT)
                    //id pour l'element que l'on va cacher
                    //vari pour les variables a seter du style ?prout="xxx"
                    {
                             
                        var form_data = new FormData();     
                        var ins = document.getElementById(idT).files.length;
                        for (var x = 0; x < ins; x++) 
                        {
                            form_data.append("file[]", document.getElementById(idT).files[x]);
                        }
                        
                        //form_data est une liste. Je rajoute un element a cette liste afin de l'envoyer dans le POST
                        form_data.append('category', category); 
                        
                        $.ajax(
                        {
                            url: 'ajax/saveNewPicture.php', // point to server-side PHP script 
                            dataType: 'text',   // what to expect back from the PHP script, if anything
                            cache: false,
                            contentType: false,
                            processData: false,
                            data: form_data,                                             
                            type: 'post',
                            xhr: function() 
                            //### CA VA PERMETTRE DE SAVOIR LE POURCETAGE DE UPLOAD DES PHOTOS
                            {
                                var myXhr = $.ajaxSettings.xhr();
                                if(myXhr.upload)
                                {
                                    //console.log("CA UPLOAD");
                                    myXhr.upload.addEventListener('progress', progress, false);
                                }
                                return myXhr;
                            },
                            success: function(php_script_response)
                            {
                                $("#presultat" + category).html(php_script_response);
                            },
                            error: function (php_script_response) 
                            {
                                $("#presultat" + category).html(php_script_response);
                            }
                        });
                    }
                    
                    function progress(e, category)
                    //### CETTE FONCTION SERT A AFFICHER LEVOLUTION DE LUPLOAD DES PHOTOS SUR LE SERVEUR.
                    {
                        if(e.lengthComputable)
                        {
                            var max = e.total;
                            var current = e.loaded;

                            var Percentage = (current * 100)/max;
                            $("#presultatDownloadprm").html("UPLOAD : " + Percentage);

                            if(Percentage >= 100)
                            {   
                                $("#presultatDownloadprm").html("UPLOAD REUSSI.");
                            }
                        }   
                    }

Upvotes: 0

Views: 35

Answers (1)

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2914

You can do something like this

myXhr.upload.addEventListener('progress', function(event){
    progress(event, parameter1, parameter2);
}, false);

Upvotes: 1

Related Questions