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;

?>
3
  • put ajax call inside .get return Commented Dec 20, 2015 at 3:12
  • Possible duplicate of Sending binary data in javascript over HTTP Commented Dec 20, 2015 at 3:17
  • thanks artm it worked ............. Commented Dec 20, 2015 at 3:40

1 Answer 1

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
        }
    });
});
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.