php.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>PHP example - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies apart from jQuery)
  8. -->
  9. <script data-jsfiddle="common" src="../demo/js/jquery.min.js"></script>
  10. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  11. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  12. <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  13. <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  14. <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  15. <script data-jsfiddle="common" src="../dist/numbro/numbro.js"></script>
  16. <script data-jsfiddle="common" src="../dist/numbro/languages.js"></script>
  17. <script data-jsfiddle="common" src="../dist/handsontable.js"></script>
  18. <!--
  19. Loading demo dependencies. They are used here only to enhance the examples on this page
  20. -->
  21. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  22. <script src="js/samples.js"></script>
  23. <script src="js/highlight/highlight.pack.js"></script>
  24. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  25. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  26. <!--
  27. Facebook open graph. Don't copy this to your project :)
  28. -->
  29. <meta property="og:title" content="PHP example - Handsontable">
  30. <meta property="og:description"
  31. content="This page loads and saves data on server. In this example, client side uses $.ajax. Server side uses PHP with PDO (SQLite)">
  32. <meta property="og:url" content="http://handsontable.com/demo/php.html">
  33. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  34. <meta property="og:image:type" content="image/png">
  35. <meta property="og:image:width" content="409">
  36. <meta property="og:image:height" content="164">
  37. <link rel="canonical" href="http://handsontable.com/demo/php.html">
  38. <!--
  39. Google Analytics for GitHub Page. Don't copy this to your project :)
  40. -->
  41. <script src="js/ga.js"></script>
  42. </head>
  43. <body>
  44. <div class="wrapper">
  45. <div class="wrapper-row">
  46. <div id="global-menu-clone">
  47. <h1><a href="../index.html">Handsontable</a></h1>
  48. </div>
  49. <div id="container">
  50. <div class="columnLayout">
  51. <div class="rowLayout">
  52. <div class="descLayout">
  53. <div class="pad" data-jsfiddle="example1">
  54. <h2>PHP example</h2>
  55. <p>This page loads and saves data on server. In this example, client side uses <b>$.ajax</b>. Server side uses
  56. <b>PHP with PDO (SQLite)</b>.</p>
  57. <p>Please note. This page and the PHP scripts are a work in progress. They are not yet configured on GitHub.
  58. Please run it on your own localhost.</p>
  59. <p>
  60. <button name="load">Load</button>
  61. <button name="save">Save</button>
  62. <button name="reset">Reset</button>
  63. <label><input type="checkbox" name="autosave" checked="checked" autocomplete="off"> Autosave</label>
  64. </p>
  65. <div id="exampleConsole" class="console">Click "Load" to load data from server</div>
  66. <div id="example1"></div>
  67. <p>
  68. <button name="dump" data-dump="#example1" data-instance="hot" title="Prints current data source to Firebug/Chrome Dev Tools">
  69. Dump data to console
  70. </button>
  71. </p>
  72. </div>
  73. </div>
  74. <div class="codeLayout">
  75. <div class="pad">
  76. <script>
  77. var
  78. $container = $("#example1"),
  79. $console = $("#exampleConsole"),
  80. $parent = $container.parent(),
  81. autosaveNotification,
  82. hot;
  83. hot = new Handsontable($container[0], {
  84. columnSorting: true,
  85. startRows: 8,
  86. startCols: 3,
  87. rowHeaders: true,
  88. colHeaders: ['Manufacturer', 'Year', 'Price'],
  89. columns: [
  90. {},
  91. {},
  92. {}
  93. ],
  94. minSpareCols: 0,
  95. minSpareRows: 1,
  96. contextMenu: true,
  97. afterChange: function (change, source) {
  98. var data;
  99. if (source === 'loadData' || !$parent.find('input[name=autosave]').is(':checked')) {
  100. return;
  101. }
  102. data = change[0];
  103. // transform sorted row to original row
  104. data[0] = hot.sortIndex[data[0]] ? hot.sortIndex[data[0]][0] : data[0];
  105. clearTimeout(autosaveNotification);
  106. $.ajax({
  107. url: 'php/save.php',
  108. dataType: 'json',
  109. type: 'POST',
  110. data: {changes: change}, // contains changed cells' data
  111. success: function () {
  112. $console.text('Autosaved (' + change.length + ' cell' + (change.length > 1 ? 's' : '') + ')');
  113. autosaveNotification = setTimeout(function () {
  114. $console.text('Changes will be autosaved');
  115. }, 1000);
  116. }
  117. });
  118. }
  119. });
  120. $parent.find('button[name=load]').click(function () {
  121. $.ajax({
  122. url: 'php/load.php',
  123. dataType: 'json',
  124. type: 'GET',
  125. success: function (res) {
  126. var data = [], row;
  127. for (var i = 0, ilen = res.cars.length; i < ilen; i++) {
  128. row = [];
  129. row[0] = res.cars[i].manufacturer;
  130. row[1] = res.cars[i].year;
  131. row[2] = res.cars[i].price;
  132. data[res.cars[i].id - 1] = row;
  133. }
  134. $console.text('Data loaded');
  135. hot.loadData(data);
  136. }
  137. });
  138. }).click(); // execute immediately
  139. $parent.find('button[name=save]').click(function () {
  140. $.ajax({
  141. url: 'php/save.php',
  142. data: {data: hot.getData()}, // returns all cells' data
  143. dataType: 'json',
  144. type: 'POST',
  145. success: function (res) {
  146. if (res.result === 'ok') {
  147. $console.text('Data saved');
  148. }
  149. else {
  150. $console.text('Save error');
  151. }
  152. },
  153. error: function () {
  154. $console.text('Save error');
  155. }
  156. });
  157. });
  158. $parent.find('button[name=reset]').click(function () {
  159. $.ajax({
  160. url: 'php/reset.php',
  161. success: function () {
  162. $parent.find('button[name=load]').click();
  163. },
  164. error: function () {
  165. $console.text('Data reset failed');
  166. }
  167. });
  168. });
  169. $parent.find('input[name=autosave]').click(function () {
  170. if ($(this).is(':checked')) {
  171. $console.text('Changes will be autosaved');
  172. }
  173. else {
  174. $console.text('Changes will not be autosaved');
  175. }
  176. });
  177. </script>
  178. </div>
  179. </div>
  180. </div>
  181. <div class="footer-text">
  182. </div>
  183. </div>
  184. </div>
  185. </div>
  186. </div>
  187. <div id="outside-links-wrapper"></div>
  188. </body>
  189. </html>