Easy Coding
  Forum Wiki Tagging Projekte Karte RSS
» Start
» All Recent Changes
» Wiki Suche
» Wiki Hilfe

Coder How To's

Algorithmen Informationen

edit SideBar

Neue Wiki Eintrage finden Sie unter easy-coding.de/wiki.

AJAX AutoSave

Hier findet ihr ein Tutorial, welches alle x Sekunden die ins Formular eingegeben Daten per AJAX an ein PHP Script übermittelt und speichert.

ajax-autosave.html
  1. <script type="text/javascript">
  2. // Die Inaktivitätszeit, nach der automatisch gespeichert wird
  3. var inactivity = 4;
  4. var timer = 0;
  5.  
  6. function Zeitaufschub()
  7. {
  8.         timer = inactivity;
  9. }
  10.  
  11. function Countdown() {
  12.         timer--;
  13.         if(timer == 0) {
  14.                 //Die Zeit ist um, bitte speichern
  15.                 autoSave();
  16.                 Zeitaufschub();
  17.         }
  18.         //Die Funktion ruft sich 1x in der Sekunde alleine auf
  19.         setTimeout("Countdown()", 1000);
  20. }
  21.  
  22. function autoSave()
  23. {
  24.         if(window.XMLHttpRequest) {
  25.                 req = new XMLHttpRequest();
  26.         }
  27.         else if(window.ActiveXObject) {
  28.                 req = new ActiveXObject("Microsoft.XMLHTTP");
  29.         }
  30.  
  31.         try
  32.         {
  33.                 inhalt = document.getElementById('inhalt').value;
  34.                 id = document.getElementById('id').value;
  35.                 req.open("GET", "save.php?id="+id+"&value="+inhalt, true);
  36.                 req.onreadystatechange = triggered;
  37.                 req.send(null);
  38.         } catch(e) {
  39.                 //nothing;
  40.         }
  41. }
  42.  
  43. function triggered() {
  44.         if ((req.readyState == 4) && (req.status == 200)) {
  45.                 document.getElementById("output").innerHTML = req.responseText;
  46.         }
  47. }
  48.  
  49. //Rufe die Funktion auf
  50. Zeitaufschub();
  51. Countdown();
  52. </script>
  53. </head>
  54. <div id="output">&nbsp;</div>
  55.  
  56. <form method="post" action="ajax-autosave.php">
  57.         <input type="hidden" id="id" value="5" />
  58.         <textarea id="inhalt" onkeyup="Zeitaufschub()"></textarea>
  59.         <br /><input type="submit" name="abschicken" />
  60. </form>
  61.  
  62. </body>
  63. </html>
save.php
  1. <?php
  2. include 'connect.php';
  3. //Hier ihr individuelles MySQL Query
  4. echo date('H:i:s').': '.$_GET['value'].' wurde in die DB mit der ID '.$_GET['id'].' eingetragen';
  5. ?>
Zuletzt geändert am 04.08.2006 10:58 Uhr
  Impressum