I am trying to save my signature on the server, but it is not saving the signature but I get the "data:image/png;base64,iVBORw..." on my js console log. Please help me to save my signature on the server.
My Js code:
function uploadSignature(mimetype) {
var dataurl = signaturePad.toDataURL(mimetype);
var blobdata = dataURLtoBlob(dataurl);
var fd = new FormData(document.getElementById("UploadForm"));
fd.append("imageData", blobdata, "filename")
/** will result in normal file upload with post name "signature" on target url **/
$.ajax({
url: "/signature_pad.php",
type: 'POST',
data: fd,
processData: false,
contentType: false,
dataType: 'html',
success: function (response) {
alert("AJAX OK: uploadSignature() ok");
console.log(response);
},
error: function (e) {
alert("AJAX ERROR: uploadSignature() fehlgeschlagen");
console.log(e);
}
});
}
My PHP codes to save the signature in the server:
<?php
if (isset($_POST['imageData'])) {
$imgData = base64_decode($_POST['imageData']);
$image_name= $_POST['image_name'];
// Path where the image is going to be saved
$filePath = '../'.$image_name.'.png';
// Delete previously uploaded image
if (file_exists($filePath)) { unlink($filePath); }
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
} else {
echo "imgData doesn't exists";
}
I am new in ajax, I couldn't understand how to send a request through ajax to save the signature on the server. Thank you so much