Page 1 of 1

Auto Saving Values

Posted: Wed Mar 21, 2012 1:36 pm
by Bigun
I have a couple of projects that could benefit from an autosaving feature. I've never attempted to implement something like this before, but I would assume it would be some kind of javascript feature since it isn't initiated by some action like a submit button or link, but rather by a value changing (like a checkbox or text field).

If anyone has a page I could reference mingling javascript and PHP to do some kind of auto saving feature, I would appreciate any help.

If I'm in the wrong sub-forum, I apologize, I just assumed javascript would be the answer here.

Re: Auto Saving Values

Posted: Wed Mar 21, 2012 1:56 pm
by tr0gd0rr
Yes, you would want to use JavaScript. In jQuery you would probably use the onchange event:

Code: Select all

var myForm = $('#MyForm')[0];
var recordId = myForm.elements.id.value;
$(myForm.elements).change(function() {
   var enc = window.encodeURIComponent;
   $.ajax({
      url: '/autosave.php',
      method: 'POST',
      data: 'column=' + enc(this.name) + '&id=' + enc(recordId) + '&new_value=' + enc(this.value)
   });
})
Then in autosave.php update the column `$_POST['column']` to the value `$_POST['new_value']` where the record id equals `$_POST['id']`. Just be sure to check that the logged-in user has permission to edit that record id!

Re: Auto Saving Values

Posted: Wed Mar 21, 2012 2:11 pm
by Bigun
What about on the fly validation of variables?

I guess I need a good resource for learning javascript.

Re: Auto Saving Values

Posted: Wed Mar 21, 2012 6:11 pm
by califdon
There are many online tutorials for Javascript, but be sure you understand that jQuery is an extra library for Javascript, and you would have to learn that syntax, as well, if you want to use it.

I would comment, though, that if you auto-save with every keystroke, you're going to be placing a pretty heavy load on your server, assuming that's where you're going to save to, and assuming that the text is more than a few characters. I would think about how often you want to auto-save, either how many characters entered, or number of seconds since the cursor entered the input box, or something.

As for validation, that's a standard thing to do with Javascript, if you don't need to validate against a database on the server.

Re: Auto Saving Values

Posted: Thu Mar 22, 2012 7:18 am
by Bigun
The data I would be saving in text field is little more than 5 or 10 characters at most (a time or date field) and I would probably initiate it via exiting the field.

I'm probably going to get the company to buy me some jQuery books.... this isn't the first time I've seen this library before.

Re: Auto Saving Values

Posted: Thu Mar 22, 2012 12:25 pm
by califdon
There's a LOT of info on jQuery online. Just google it. jQuery is worth learning, for sure, but it's not essential for the task you originally asked about. I'm still not clear about where you intend to "save" the data. In a server-side database? In a session variable? The technique you use depends entirely on what it is you are trying to do. Your user is entering text in a web form? What is it you are trying to achieve? Do you really mean "save" or are you merely trying to retain the data, say, in the event that you later need to restore the data in the form in case of rejection of the first submission? There are several scenarios here, and each one will have a different solution that is appropriate.