I tried to send the data in localstorage to php with ajax but I get the error of
undefined index data
The code generate a dataurl which is stored in the localstorage. When the user click on the link the pdfGen.php is called where I want to use the data in localstrage.
chart.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#download').click(function() {
$.ajax({
type: "POST",
url: "pdfGen.php",
data: {data:'hello'},
success: function(data) {
alert("hi");
}
});
});
}); //END $(document).ready()
</script>
<script>
//<![CDATA[
(function() {
window.onload = function(){
html2canvas(document.getElementById('chart'), {
"onrendered": function(canvas) {
var img = new Image();
img.onload = function() {
img.onload = null;
console.log(canvas.toDataURL("image/png"));
window.localStorage.setItem("imgURL", canvas.toDataURL("image/png"));
};
img.onerror = function() {
img.onerror = null;
if(window.console.log) {
window.console.log("Not loaded image from canvas.toDataURL");
} else {
//alert("Not loaded image from canvas.toDataURL");
}
};
img.src = canvas.toDataURL("image/png");
}
});
};
})();
//]]>
</script>
<body>
<a href="pdfGen.php" id="download" >Report</a>
..more code to generate the chart
</body>
The download button calls the pdfGen.php script which uses fpdf to generate a report.
pdfGen.php
<?php
echo $_POST['data']; //gives error
/*$pdf = new FPDF();
$pdf->AddPage();
//over here I want to add the image from the chart.php page whose data url is now in the localstorage.
..more code to generate report
$pdf->output();*/
?>
How do I get the data inside the php script? I try to make the ajax call but I get undefined index in pdfGen.php script. I got the alert HI but could not get the data on the server. It does not seem to work.