2

I made this code to make image upload function but the variable is not getting posted by ajax please help me !! Jquery()

<script type="text/JavaScript">
$(document).ready(function() {
    $("#btn").click(function() {
    $.get("i.png", function(response) {


 var img = response;
}); 

$.ajax({
    type: "POST",
    url: 'lol.php',
    data: {r: img},
    success: function(data){
        $("#ol").html(data)

    }

});
return false;
});
});

</script>

PHP Code

<?php
$conn = mysqli_connect("localhost","root","","image");
$r = $_POST['r'];
echo $r;

?>

1 Answers1

0

If you only want to make an image upload (and not exactly match "binary upload")

I suggest you to use the proper functional and native input type file.

In a html form, put an :

<input type="file" id="upload">
<img src="blank.png" title="upload-preview">

and in you javascript:

this will load the preview thumbnail selected

fileInput.addEventListener("change", function(event)
{
    var file = $("#upload")[0].files[0];
    $(".upload-preview").attr('src', window.URL.createObjectURL(file));
});

and when you click the button, that will send the image

with a "multipart/form-data" Content-Type

$("#btn").on("click", function()
{
    var inputs = new FormData();
    inputs.append("upload", $("#upload")[0].files[0]);

    $.ajax
    ({
        url:"your.php",
        type:"POST",
        data: inputs,
        cache: false, // don't cache the image
        processData: false, // Don't process the files
        contentType: false,
        success: function(response)
        {
            // next instructions
        }
    });
});
MTroy
  • 897
  • 9
  • 20