Auto Saving Values

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Auto Saving Values

Post 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.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Auto Saving Values

Post 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!
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Auto Saving Values

Post by Bigun »

What about on the fly validation of variables?

I guess I need a good resource for learning javascript.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Auto Saving Values

Post 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.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Auto Saving Values

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Auto Saving Values

Post 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.
Post Reply