-1

This returns "Ok." alert and array(0) { }. How can I get jQuery image variable?

<script>
    var image = canvas.toDataURL('image/png');
    $.ajax({
        type: 'POST',
        data: image,
        success: function() {
            alert('Ok.');
        }
    });
</script>

<?php
    var_dump($_POST);
?>
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
wavix
  • 13
  • 6

3 Answers3

0

You're posting to the same page via AJAX. On the original page you will still see array(0) { } but the page requested via AJAX will contain correct POST data in the response.

Also the correct code to submit image parameter is shown below.

var image = canvas.toDataURL('image/png');
$.ajax({
    type: 'POST',
    data: { 'image' : image },
    success: function(){
        alert('Ok.');
    }
});

See this JSFiddle for demonstration.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • @wavix, You're seeing `array(0){ }` because you're doing AJAX request which doesn't update the current page. In the response to AJAX request your page produces correct output, which can be verified using debugging tools of your favorite browser. Consider switching to form submission using POST if you want to see the output or adjust your PHP script to handle AJAX POST differently than regular GET. It all depends on what you're trying to do. – Gyrocode.com May 17 '15 at 21:34
0

If you just want to upload an image to PHP Web server, via AJAX, You need to have written your Web server to catch the Image data which you have sent.

I suggest you to follow two links:

  1. StackOverflow answer
  2. Tutorial Link

Hope this may help you to get resolved your problem!

Cheers!

Community
  • 1
  • 1
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
-1

Try this, it will get the variable at the server side

<script>
$.ajax({
        url: "//php serverside url",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(data)
        {
        //sucess    
        },
        error: function(){

   }
   });
</script>
Ikelie Cyril
  • 130
  • 7