Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add additional POST parameters to ajax file upload?

I'm using ajax file upload javascript and php script to upload an image. It works satisfactorily with $_FILES but I need to send some additional data to the processing script. The form HTML looks like:

<form id="image1" action="" method="post" enctype="multipart/form-data">
  <label>image 1?</label>
  <p><input type="file" class="saveImage" name="image1" value="<?php echo $something; ?>" id="<?php echo $id; ?>" additional_info="some data" /></p>
  <p> <input type="submit" value="Upload" class="submit" /></p>
</form>

I need to be able to pass a variable id and some other data, call it "additional_data" to the php script, then process it in my php script using $additional_data = $_POST['additional_data']. The javascript I'm using is:

    <script>
    $(document).ready(function (e) {
      $("#image1").on('submit',(function(e) {
        e.preventDefault();
        $("#message").empty();
        $('#loading').show();
        var DATA=$(this).val();
        var ID=$(this).attr('id');
        var ADDL=$(this).attr('additional_data');
        var dataString = 'image1='+DATA+'&id='+ID+'&additional_info='+ADDL;
        $.ajax({
          url: "uploadFile.php",  
          type: "POST",        
          // data:  new FormData(this), 
          data:  new FormData(this,dataString),
          contentType: false,   
          cache: false,   
          processData:false,        
          success: function(data)    
          {
            $('#loading').hide();
            $("#message").html(data);
          }
        });
      }));
    });
    </script>

It doesn't send the dataString, only the FILES array.

like image 432
Bob M Avatar asked Mar 19 '15 22:03

Bob M


People also ask

How do I send a post request in Ajax?

Send Http POST request using ajax()In the options parameter, we have specified a type option as a POST, so ajax() method will send http POST request. Also, we have specified data option as a JSON object containing data which will be submitted to the server.


1 Answers

I also wanted to do the same thing. Here's my solution :

The JS part :

var file_data = this.files[0];
    file_data.name = idaviz +'.pdf';
    var form_data = new FormData();
    form_data.append("file", file_data);
    form_data.append('extraParam','value231');
    console.log(file_data);
    console.log('here');
    var oReq = new XMLHttpRequest();
    oReq.open("POST", "ajax_page.php", true);
    oReq.onload = function (oEvent) {
        if (oReq.status === 200) {
            console.log('upload succes',oReq.responseText);
        } else {
            console.log("Error " + oReq.status + " occurred when trying to upload your file.<br \/>");
        }
    };

    oReq.send(form_data);
});

The PHP part:

echo $_REQUEST['extraParam']; //this will display "value231"
var_dump($_FILES['file']);   //this will display the file object

Hope it helps.

Addition info about extra parameters on formData can be found here!

like image 111
Alin Panainte Avatar answered Oct 14 '22 04:10

Alin Panainte