3

I know there has been several questions regarding this very question and I have combed through many of them and have not found a right way to accomplish the goal properly. Essentially, I am trying to capture multiple file inputs in the form and push them to PHP. I have the PHP part at least figured out but its the Jquery im struggling with. One of the main problems I have noticed through debugging is that my action parameter is not getting through at all. My code I posted is below. Any help would be greatly appreciated. Thanks.

$(document).ready(function () {
  //if submitting imgages form
  $('#submit').click(function () {
    // match anything not a [ or ]
    regexp = /^[^[\]]+/;
    var fileInput = $('.putImages input[type="file"]');
    var fileInputName = regexp.exec(fileInput.attr('name'));

    // make files available
    var data2 = new FormData(fileInput);
    jQuery.each($(fileInput)[0].files, function (i, file) {
      data2.append(fileInputName + '[' + i + ']', file);
    });

    //organize data
    var data = new FormData($('input[name^="uploadFile"]'));
    jQuery.each($('input[name^="uploadFile"]')[0].files, function (i, file) {
      data.append(i, file);
    });

    $.ajax({
      type: 'GET',
      url: 'productadminupdate.php',
      data: data2 + "&action=" + 'images',
      cache: false,
      contentType: false,
      processData: false,
      success: function (response) {
        $('#imgform_result').html(response).fadeIn();
      }
    });

    return false;
  });
});
1
  • The only way you could achieve what you want is to wrap each input[name^="uploadFile"] in a <form> when you the user drops. Then we can have a generic submit function. When they click it, we prevent the default functionality. Iterate over all the form elements, take the data we need, then use $('form').submit() to perform the normal functionality. Commented Sep 2, 2012 at 1:12

1 Answer 1

1

I don't know if I got it, but when people think about ajax uploader, they don't really know that. In reality, it is not JavaScript doing the job. The technique exists in targeting the action of the form to an Iframe. This Iframe should be hidden Here is an example:

<form method='post' enctype='multipart/form-data' action='YOUR_FILE.php' target='upload_iframe'>        
    <label>photo1:</label>
    <input type='file' name='photo1'/>
    <label>photo2:</label>
    <input type='file' name='photo2'/>
    <label>photo3:</label>
    <input type='file' name='photo3'/>
    <label>photo4:</label>
    <input type='file' name='photo4'/>
    <label>photo5:</label>
    <input type='file' name='photo5'/>
    <input type='submit' value='Send'/>
    </form>
</div>
<iframe name='upload_iframe'></iframe>

So in the Iframe, your php code will make the upload, getting the information using $_FILES.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.