I am trying to save an image created and placed in canvas, I have the following:
html:
<canvas id="example" class="test" style="width:600px;" data-id="0"></canvas>
<form name="saveForm" method="post">
<input id="save" type="submit" value="save">
<input type="hidden" id="imgData" name="imgData" value="">
</form>
Jquery:
$(document).on("click", "#save", function() {
document.getElementById('imgData').value = canvas.toDataURL('image/png');
document.forms["saveForm"].submit();
});
PHP:
if($_POST['imgData']){
// echo "here"; die; // tried this but not firing so no data getting to hidden field
$upload_dir = '/home/mydir/public_html/pages/test/imgEdit/';
$img = $_POST['imgData'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir."image_name.png";
$success = file_put_contents($file, $data);
}
But its simply not working, nothing happens and no errors in inspector. Clearly from the echo test in the PHP the image data is not populating the hidden field on jquery click.
What am I missing?