1

I have this form

<form id="uploadFile" enctype="multipart/form-data">
    <h3>Upload a file:</h3>
    <input type="file" name="fileToUpload" id="fileToUpload" class="fileToUpload"/>
    <input type="submit" id="submit" value="Upload" name="submit"/>
</form>

And this jQuery code for the submit function (all in the same php file):

  $('#uploadFile').submit(function()
  {
    $old=$("#activity").html();

    event.preventDefault();
    $.ajax(
    { 
      url: "upload.php",
      type: 'POST',
      data: new FormData(this),
      contentType: false,
      cache: false,
      processData: false,
      success: function(data)
      {
        $('.info').html(data);   
        $('.info').show();

        $("#content").load("this_same_file.php"); 
      }
    });
  }); 

It works fine. What I need to do is to 'automate' the 'submit' call. I mean, once I clicked OK on the pop-up file window, I would like to submit automatically the form.

I tried to follow the suggestions posted on the thread How do I auto-submit an upload form when a file is selected? but the only thing I can do is a window.alert advertising me that I have clicked the OK button of the pop-up window.

How can I implement this feature in my code ? Thank you in advance for your help.

Community
  • 1
  • 1

5 Answers5

2

You need to trigger the submit action after the change event on input:file happens,

$('#fileToUpload').change(function() {
    $('#submit').click();
    // or $('#uploadFile').submit();
});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
0

you need to make a trigger (doc)

Haroldas
  • 964
  • 3
  • 9
  • 36
0
$("#fileToUpload").change(function() {$("#uploadFile").submit();});

Beside I think you have to implement Jquery ajaxForm to send form with file input.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
0

try

$("#fileToUpload").live( 'change', function () {
 $('#uploadFile').submit();
});
Saty
  • 22,443
  • 7
  • 33
  • 51
-1

You can upload the image by calling a javascript function on change event or on click event.

<input type="file" onchange="uploadSharepic();" id='uploadBtn' value="Upload">

And in script

function uploadSharepic(){
    var val = $('#uploadBtn').val();
    $('#uploadshareFile').val(val);
    switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
        case 'gif': case 'jpg': case 'png': case 'jpeg':
            $( "#uploadShare_image" ).submit();
            break;
        default:
            $('#uploadBtn').val("");
            $('#uploadshareFile').val("");
            $('.sharerror').removeClass('DisplayNone');
            $('.sharerror').addClass('DisplayBlock');
            $('.sharerror').html('Plese select image of type (gif, jpg, png).');
            break;
    }
}
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Gaurav Singh
  • 189
  • 2
  • 7