0
function UpdateUserDetails() 
{
   var title = $("#title1").val();
   var store = $(".search-box").val();
   var category= $("#category").val();
   var descp=$("#descp1").val();
   var price=$("#price1").val();
   var value=$("#value1").val();
   var location=$("#location1").val();
   var url=$("#url1").val();
   var id = $("#hidden_user_id").val();

   $.post("update.php", {
        id: id,
        title:title,
        store:store,
        category:category,
        descp:descp,
        price:price,
        value:value,
        location:location,
        url:url

    },
    function (data, status) {
        $("#update_user_modal").modal("hide");
    }
);
}

The above is my code where it fetches values from html form and then sends to update.php file. How can i send a image file through this function along with all the variables? should i send it as file itself or by encoding it? Help me out!!

SUNIL
  • 107
  • 1
  • 10

2 Answers2

0

Very basic approach is:

  1. Convert image to base64-data How to convert image into base64 string using javascript
  2. Send it via ajax as string and on server side just base64_decode($b64Image) and save raw data to file

But don't forget to set post_max_size in php.ini and/or in Nginx body_size (i forget how it named)

Community
  • 1
  • 1
edwardstock
  • 168
  • 2
  • 10
0

You can use FormData to pass files to your php file through ajax. For example:

$(':file').change(function() {
var data = new FormData();
jQuery.each(jQuery(':file')[0].files, function(i, file) {
    data.append('file[]', file);
});

Then you can send it by ajax:

    jQuery.ajax({
    url: 'your url',
    data: data,
    cache: false,
    dataType: 'json',
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
       //your code
    }
});

Now you can treat these files like $_FILES['file'] array on your php.