DragonStyle
DragonStyle

Reputation: 145

jQuery Ajax Upload and a ID

Hello I got this code:

$(document).ready(function(){

$('#upload-file-selector').change(function(){

     var $currID = $("#upload-file-selector").attr('data-uid')

    $(this).simpleUpload("uploadFile.php", {

        start: function(file){
            //upload started
            $('#filenameD').html(file.name);
            $('#progressD').html("");
            $('#progressBarD').width(0);
        },

        progress: function(progress){
            //received progress
            $('#progressD').html("Progress: " + Math.round(progress) + "%");
            $('#progressBarD').width(progress + "%");
        },

        success: function(data){
            //upload successful
            $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
        },

        error: function(error){
            //upload failed
            $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
        }

    });

});

});

It works with this plugin: SimpleUpload

As you see I have the var currID. How can I send this ID with the file to the PHP? How can I bind the var to the JSON-String?

Upvotes: 0

Views: 130

Answers (1)

Twisty
Twisty

Reputation: 30893

If you want to pass the $currID via POST, use the data option for SimpleUpload.

http://simpleupload.michaelcbrook.com/#settings

data object - A set of key-value pairs containing the POST data you would like to send to the server along with each file. This is not automatically populated by any surrounding forms.

Using your code:

$(document).ready(function(){
  $('#upload-file-selector').change(function(){;
    var $currID = $("#upload-file-selector").attr('data-uid')
    $(this).simpleUpload("uploadFile.php", {
      data: {
        "id": $currID
      },
      start: function(file){
        //upload started
        $('#filenameD').html(file.name);
        $('#progressD').html("");
        $('#progressBarD').width(0);
      },
      progress: function(progress){
        //received progress
        $('#progressD').html("Progress: " + Math.round(progress) + "%");
        $('#progressBarD').width(progress + "%");
      },
      success: function(data){
        //upload successful
        $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
      },
      error: function(error){
        //upload failed
        $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
      }
    });
  });
});

Upvotes: 1

Related Questions