Search code examples
javascriptjqueryajaxjquery-upload-file-plugin

javascript variable scope in this particular instance


In javascript/jQuery, before a file upload plugin is called, a variable is set:

doc_type = "q";

Then, the plugin is initialized. Within the plugin options is: onSelect:, which is called when files are selected. Code looks like this:

var doc_type = "q";
$(function(){
    var project_num = $("#pnum").val();
    var uploadObj = $("#fileuploader").uploadFile({
        url: "upload_files_processor.php",
        method: "POST",
        onSelect: function(){
            doc_type = "W";
            //Or:
            //doc_type = $('#hidden_input').val();  <-- What I really need to do
            return true;
        },
            allowedTypes:"pdf,doc,docx,xls,xlsx,ppt,pptx,bmp,jpg,png,zip",
        fileName: "myfile",
        formData: {"project_num":project_num,"doc_type":doc_type},
        multiple: true,
        autoSubmit: true,
        showStatusAfterSuccess:false,
        onSuccess:function(files,data,xhr) {
            //Refresh documents table
        },
    });
}); //END document.ready()

Problem:

In the upload processor upload_files_processor.php, the received doc_type value is:

$doc_type = $_POST["doc_type"];  // q

How can I receive the value W ?

References: heyageek jquery upload file plugin website -- click on API & Options tab


Solution

  • In your code, instead of formData: {"project_num":project_num,"doc_type":doc_type}, use this:

    dynamicFormData: function()
    {
        return {
            project_num: $("#pnum").val(),
            doc_type: $('#hidden_input').val()
        };
    },
    

    And remove the lines var doc_type = "q"; and var project_num = $("#pnum").val()