-1

I'am sorry if this question is stupid , but how I can save canvas image to database via php I've searched a lot but did not found anything working and useful
and here is the code that I use it just popup print form

  <script>
   function print(){
    var canvas1 = document.getElementById("testCanvas");
    var ctx1 = canvas1.getContext("2d");
    var img = canvas1.toDataURL("image/png");
    img = encodeURIComponent(img);

    $.ajax({
        url: 'upload.php',
        data: { data: img },
                type: 'post',
                success: function(data) {
                    console.log(data);
                    alert("Done");
                }
    });
    </script>
    <button onclick="print()">Click me</button><br>

Upload.php

<?php

    $data = $_POST['data'];

    $server = "localhost";
    $username = "root";
    $password = "";
    $database = "sports";
    $bd = mysql_connect($server, $username, $password) or die("1");
    $ok = mysql_select_db($database, $bd) or die("2");

     $sql = "INSERT INTO image (myimage) VALUES ($data)";
    if( ! mysql_query($sql) )
   {
       echo "Error: " . mysql_error();
   }

    echo $qry;

?>
Sara Love
  • 47
  • 2
  • 9

1 Answers1

0

You can, using AJAX. The process will be :

  1. Javascript handle the canvas to create the image
  2. Javascript call a url generated by your PHP
  3. Your PHP handle the date send via POST method by the Javascript

Here is an answer that can help you : uploading canvas context as image using ajax and php

Community
  • 1
  • 1
Ivan Gabriele
  • 6,433
  • 5
  • 39
  • 60