get img source (from dataURL canvas) to another page

1.5k Views Asked by At

Sorry maybe the title isn't very clear on what I try to get done.

Let me explain. I have a html page on which I have a button, as soon as I click the button, a php page should be run (in background). This page generates (from a database) a canvas and that canvas is turned into an encoded image (base64) and this is added to the img source of that php page (this works). Now what I want is to get that img source and display it on the html page (where the button is).

Let me know if you need any sample code.

I hope someone can help me with this. Thanks

2

There are 2 best solutions below

2
On

From the little code you posted, I would think it would look something like this, allthough not sure?

......
var dataURL = canvas.toDataURL(); 
document.getElementById("canvasImg").src = dataURL;
document.getElementById("ImgCode").value = dataURL;
};
</script>
</head>
<body>
<canvas id="myCanvas" width="73" height="103" style="display:none;"></canvas>

<? $tag = '<img id="canvasImg">'; 
<? $tag2 = '<input id="ImgCode" type="text">';

echo $tag;?>
echo $tag2;?>

</body>
</html>

You'll probably have to style and position the input tag, or replace with a div, or whatever you like really, just use innerHTML or something similar instead of value.

1
On

If you are using jQuery you could use the .load function :

$('#result').load('ajax/test.php', function() {
  alert('Load was performed.');
});

where #result is the ID of the button or a div near it .... the ajax/test.php would be your script and the alert within the function is the success callback (can be removed if not needed)