Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. chart.php
  2. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  3.  
  4. <script type="text/javascript">
  5. $(document).ready(function() {
  6. $('#download').click(function() {
  7. $.ajax({
  8. type: "POST",
  9. url: "pdfGen.php",
  10. data: 'hello',
  11. success: function(data) {
  12. alert("hi");
  13. }
  14. });
  15. });
  16. }); //END $(document).ready()
  17. </script>
  18.  
  19. <script>
  20. //<![CDATA[
  21. (function() {
  22. window.onload = function(){
  23. html2canvas(document.getElementById('chart'), {
  24. "onrendered": function(canvas) {
  25. var img = new Image();
  26. img.onload = function() {
  27. img.onload = null;
  28. console.log(canvas.toDataURL("image/png"));
  29. window.localStorage.setItem("imgURL", canvas.toDataURL("image/png"));
  30. };
  31. img.onerror = function() {
  32. img.onerror = null;
  33. if(window.console.log) {
  34. window.console.log("Not loaded image from canvas.toDataURL");
  35. } else {
  36. //alert("Not loaded image from canvas.toDataURL");
  37. }
  38. };
  39. img.src = canvas.toDataURL("image/png");
  40. }
  41. });
  42. };
  43. })();
  44. //]]>
  45. </script>
  46. <body>
  47. <a href="pdfGen.php" id="download" class="button">Report</a>
  48. ..more code to generate the chart
  49. </body>
  50.  
  51. <?php
  52. echo $_POST['data']; //gives error
  53. /*$pdf = new FPDF();
  54. $pdf->AddPage();
  55.  
  56. //over here I want to add the image from the chart.php page whose data url is now in the localstorage.
  57. ..more code to generate report
  58. $pdf->output();*/
  59. ?>
  60.  
  61. $('#download').click(function(e) {
  62. e.preventDefault();
  63. $.ajax({
  64. type: "POST",
  65. url: "pdfGen.php",
  66. data: 'data=hello',
  67. success: function(data) {
  68. alert("hi");
  69. }
  70. });
  71. });
  72.  
  73. <?php
  74.  
  75. session_start();
  76.  
  77. $key = 'my-car'; // Your localstorage key
  78. $reload = false;
  79.  
  80. $client = (isset($_GET[$key]) && $_GET[$key] !== 'null') ? $_GET[$key] : null;
  81. $server = isset($_SESSION[$key]) ? $_SESSION[$key] : null;
  82.  
  83. $_SESSION[$key] = $client; // Now stored in php´s session variable $_SESSION['my-car']
  84.  
  85. echo $client !== $server ? 'true' : 'false'; // Tells js to reload if data was not synced
  86.  
  87. <script>
  88. window.localStorage.setItem('my-car', 'Tesla'); // Set to whatever you want
  89. var key = 'my-car';
  90. var data = {};
  91. data[key] = window.localStorage.getItem(key);
  92. jQuery.get(
  93. location.protocol + '//' + location.host + '/retrieve.php',
  94. data
  95. ).done(function (reload) {
  96. if (reload === 'true') {
  97. location.reload(); // When page reloads, php´s session contains correct localstorage data
  98. }
  99. });
  100. </script>
  101.  
  102. <?php
  103.  
  104. start_session(); // Dont forget this
  105.  
  106. echo $_SESSION['my-car']; // Will print 'Tesla'
  107.  
  108. <?php
  109.  
  110. start_session();
  111.  
  112. $_SESSION['my-car'] = 'HONDA';
  113.  
  114. // This line is required to sync session with js
  115. echo "<script>window.localStorage.setItem('my-car', " . $_SESSION['my-car'] . ");</script>";
Add Comment
Please, Sign In to add comment