PHP auto-save code

Reply

Join Date: Dec 2007
Posts: 52
Reputation: mgn2683 is an unknown quantity at this point 
Solved Threads: 0
mgn2683's Avatar
mgn2683 mgn2683 is offline Offline
Junior Poster in Training

PHP auto-save code

 
0
  #1
Dec 21st, 2007
I'm new to working with PHP and I am in need of some help. I am currently working on setting up a test using Dreamweaver so that users can log in, then go to the question page, and from there go to a page with a form to answer that question. I had the host up the garbage collection time from 24 minutes to 240, so that the user won't be timed out while working on the reply. I would like install a fail safe so they won't lose all their work. I found an auto-save code on http://jetlogs.org/2007/11/11/auto-saving-with-jquery/
I've been playing around with the code supplied from there, but so far no luck. Does anyone have any advice or is familiar at all with auto-saving with php? I appreciate any help, thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #2
Dec 22nd, 2007
Post your code and I'll give you a hand. The auto-saving is easy to do, and there is no reason to use jQuery, if this is all you want to do. Note: I have not used jQuery myself in any significant way (nor Dreamweaver, for that matter), but I have written many scripts that implement various levels of auto-save functionality.

(Nor do you even need PHP to do specifically what you're asking. Using JavaScript, you can auto-save a cookie in the browser that will store the value, then send it across the wire when the user hits submit. I think it's a more elegant solution, but it would require little more coding (but not much)).
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #3
Dec 22nd, 2007
here you go

edit: code does NOT save to a cookie, btw

  1. <?PHP
  2. /******************************************************************************
  3. To be able to run this sample code, you need to have the mysql database
  4. and insert a dummy record:
  5.  
  6.   CREATE DATABASE autosave;
  7.  
  8.   CREATE TABLE asave(
  9.   user_id INT,
  10.   saved_text TEXT
  11.   );
  12.  
  13.   INSERT INTO asave(user_id, saved_text) VALUES (1, '');
  14.  
  15. ******************************************************************************/
  16.  
  17. //replace with your user/pass to get a connection to your database
  18. $db = mysql_connect("localhost:3306", "****" , "****");
  19. mysql_select_db("autosave",$db) or die(mysql_error());
  20.  
  21. //your user_id is probably stored in your session...just replace
  22. $user_id = 1;
  23.  
  24. if($_SERVER["REQUEST_METHOD"]=="POST"){
  25.  
  26. // Make sure you escape your saved data as you need it
  27. // It is not done here, in the interest of brevity
  28.  
  29. $saved_text = mysql_real_escape_string($_POST["saved_text"]);
  30.  
  31. // Assume an entry in the DB table exists for this user.
  32.  
  33. $sql = "UPDATE asave SET saved_text = '".$saved_text."' ";
  34. $sql.= "WHERE user_id = $user_id";
  35.  
  36. mysql_query($sql) or die(mysql_error().$sql);
  37.  
  38. echo "Your data has been saved ".date("h:m:s A");
  39. exit;
  40. }
  41. //else implied by exit
  42.  
  43. // Get saved_text from the DB to display in the textarea
  44.  
  45. $sql = "SELECT saved_text FROM asave WHERE user_id = $user_id";
  46. $rs = mysql_query($sql) or die(mysql_error().$sql);
  47. $arr = mysql_fetch_array($rs);
  48. $saved_text = $arr["saved_text"];
  49. ?>
  50. <html>
  51. <head>
  52. <script type="text/javascript">
  53. function init(){
  54. window.setInterval(autoSave,10000); // 10 seconds
  55. }
  56. function autoSave(){
  57.  
  58. var saved_text = document.getElementById("saved_text").value;
  59.  
  60. var params = "saved_text="+saved_text;
  61. var http = getHTTPObject();
  62. http.onreadystatechange = function(){
  63. if(http.readyState==4 && http.status==200){
  64. msg = document.getElementById("msg");
  65. msg.innerHTML = "<span onclick='this.style.display=\"none\";'>"+http.responseText+" (<u>close</u>)</span>";
  66. }
  67. };
  68. http.open("POST", window.location.href, true);
  69. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  70. http.setRequestHeader("Content-length", params.length);
  71. http.setRequestHeader("Connection", "close");
  72. http.send(params);
  73. }
  74.  
  75. //cross-browser xmlHTTP getter
  76. function getHTTPObject() {
  77. var xmlhttp;
  78. /*@cc_on
  79.   @if (@_jscript_version >= 5)
  80.   try {
  81.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  82.   }
  83.   catch (e) {
  84.   try {
  85.   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  86.   }
  87.   catch (E) {
  88.   xmlhttp = false;
  89.   }
  90.   }
  91.   @else
  92.   xmlhttp = false;
  93.   @end @*/
  94.  
  95. if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  96. try {
  97. xmlhttp = new XMLHttpRequest();
  98. } catch (e) {
  99. xmlhttp = false;
  100. }
  101. }
  102.  
  103. return xmlhttp;
  104. }
  105. </script>
  106. </head>
  107. <body onload="init();">
  108. <span id="msg" style="cursor:pointer;"></span>
  109. <form method="POST">
  110. <textarea id="saved_text" name="saved_text" rows="10" cols="100"><?PHP echo $saved_text;?></textarea>
  111. <br/>
  112. <input type="submit" value="save now" />
  113. </form>
  114. </body>
  115. </html>
Last edited by bill mac; Dec 22nd, 2007 at 8:05 pm. Reason: note re: cookie
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 52
Reputation: mgn2683 is an unknown quantity at this point 
Solved Threads: 0
mgn2683's Avatar
mgn2683 mgn2683 is offline Offline
Junior Poster in Training

Re: PHP auto-save code

 
0
  #4
Dec 26th, 2007
After doing some searching around forums some code I found that might work was
<script>
// auto-save the draft every 5 minutes
setInterval( "rcmail.command('savedraft','',null)", 5*60*60*1000 );
</script>

although I would have to change the "rcmail.command" portion to something that would fit my current project. And is there any way I could install a timer so that the user could see how much time remains on their session? Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #5
Dec 26th, 2007
Originally Posted by mgn2683 View Post
After doing some searching around forums some code I found that might work was
<script>
// auto-save the draft every 5 minutes
setInterval( "rcmail.command('savedraft','',null)", 5*60*60*1000 );
</script>
5 * 60 * 60 * 1000 ms == 5 HOURs, not 5 minutes.

But, that is essentially it.

1. Write the code that does the saving, 2. Call that code every 30 seconds (or whatever).
Originally Posted by mgn2683 View Post
And is there any way I could install a timer so that the user could see how much time remains on their session? Thanks for the help.
Do something similar as above.

Write a function that will use DHTML to decrement a visible timer and call that function every second.
  1. <html><head><script>
  1. var spanCounter = ""; // HTML elem that shows counter's value
  2. var counter; // An integer that just decrements
  3. var saveInterval = -1; // Auto-save frequency
  4.  
  5. function init(si){
  6. spanCounter = document.getElementById("spanCounter");
  7.  
  8. saveInterval = si;
  9. spanCounter.innerHTML = si;
  10. counter = si;
  11.  
  12. setInterval(countDown, 1000); // once a second
  13. }
  14. function countDown(){
  15.  
  16. if(counter == -1){
  17. counter = saveInterval; // reset the counter
  18. //autoSave(); // or whatever your function is named
  19. }
  20.  
  21. spanCounter.innerHTML = (counter--); // decrement counter, and paint
  22. }
  1. </script></head>
  2. <body onload="init(5);">
  3. <span id="spanCounter"><span>
  4. </body>
  5. </html>
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 52
Reputation: mgn2683 is an unknown quantity at this point 
Solved Threads: 0
mgn2683's Avatar
mgn2683 mgn2683 is offline Offline
Junior Poster in Training

Re: PHP auto-save code

 
0
  #6
Dec 26th, 2007
mysql_query($sql) or die(mysql_error().$sql);

echo "Your data has been saved ".date("h:m:s A");
exit;
}
//else implied by exit

// Get saved_text from the DB to display in the textarea

$sql = "SELECT saved_text FROM asave WHERE user_id = $user_id";
$rs = mysql_query($sql) or die(mysql_error().$sql);
$arr = mysql_fetch_array($rs);
$saved_text = $arr["saved_text"];
?>
<html>
<head>
<script type="text/javascript">
function init(){
window.setInterval(autoSave,10000); // 10 seconds
}
function autoSave(){

var saved_text = document.getElementById("saved_text").value;

var params = "saved_text="+saved_text;
var http = getHTTPObject();
http.onreadystatechange = function(){
if(http.readyState==4 && http.status==200){
msg = document.getElementById("msg");
msg.innerHTML = "<span onclick='this.style.display=\"none\";'>"+http.responseText+" (<u>close</u>)</span>";
}
};
http.open("POST", window.location.href, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
}

//cross-browser xmlHTTP getter
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}

return xmlhttp;
}
</script>
</head>
<body onload="init();">
<span id="msg" style="cursor:pointer;"></span>
<form method="POST">
<textarea id="saved_text" name="saved_text" rows="10" cols="100"><?PHP echo $saved_text;?></textarea>
<br/>
<input type="submit" value="save now" />
</form>
</body>
</html>
[/code][/QUOTE]

Thanks for all your help so far! One problem that keeps occurring is that the autosave function only appears to work when I hit "save". I refresh the database and the text will be there, but only when save has been hit. If I was to remove the button, hypothetically the function would still work. Do you have any insight on why this is happening? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #7
Dec 26th, 2007
Replace:
  1. http.onreadystatechange = function(){
  2. if(http.readyState==4 && http.status==200){
  3. msg = document.getElementById("msg");
  4. msg.innerHTML = "<span onclick='this.style.display=\"none\";'>"+http.responseText+" (<u>close</u>)</span>";
  5. }
  6. };
with:
  1. http.onreadystatechange = function(){
  2. if(http.readyState==4){
  3. if(http.status!=200){ // 200 = HTTP OK
  4. alert(http.responseText);
  5. }else{
  6. msg = document.getElementById("msg");
  7. msg.innerHTML = "<span onclick='this.style.display=\"none\";'>"+http.responseText+" (<u>close</u>)</span>";
  8. }
  9. }
  10. };

That should give you a good start of where to look. Anyway I can execute your code over the Internet?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #8
Dec 26th, 2007
Originally Posted by mgn2683 View Post
One problem that keeps occurring is that the autosave function only appears to work when I hit "save". I refresh the database and the text will be there, but only when save has been hit. If I was to remove the button, hypothetically the function would still work. Do you have any insight on why this is happening? Thanks.
Yes, you could remove the button, and it should still work. As you can see, there is an AJAX POST every 10 seconds (i.e., the auto-save), and there is a regular HTML form that POSTs the data when submitted.

Hopefully the code I posted above this will let you know where the problem is. The code works on my setup, so I'll have to get some specific environment info from you, as well as any pertinent error messages.

Also, if you're not using Firefox w/ the Firebug extension (or Web Developer Toolbar), I highly recommend them. These tools help a lot when it comes to debugging javascript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1
Reputation: shishir.singh is an unknown quantity at this point 
Solved Threads: 0
shishir.singh shishir.singh is offline Offline
Newbie Poster

Re: PHP auto-save code

 
0
  #9
Sep 24th, 2008
Hi bill mac,

Your info is very helpfull for me , could you please help me to use this autosave feature for multiple textarea.

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC