Frage

I'm building a very simple text editor for writers as a web app, it's essentially a glorified textarea, but I need it to autosave the content of the text area so the user can leave the page and come back without having to start over. The problem is, I know very little about PHP, but I do know that I'll need it in some form. Any tips/directions/help you can give would be great.

War es hilfreich?

Lösung 2

but I need it to autosave the content of the text area so the user can leave the page and come back without having to start over.

You can do the purpose in php with ajax and session[with storage in database](or cookie) with a token but here's a simpler approach. (assumig you have the client side storage option)

take a look at sisyphus

from the intro:

With Sisyphus on site you just reopen page in your modern (with HTML5 support) browser and see all your changes at that forms. It's lightweight (3.5 KB) jQuery plugin uses Local Storage to prevent your work being lost.

supported bowsers:

  • Chrome 4+
  • Firefox 3.5+
  • Opera 10.5+
  • Safari 4+
  • IE 8+
  • IE 6 & 7 with jStorage on page loaded before Sisyphus

Andere Tipps

Its hard to give you concrete example without seeing any actual code. But this is how you can get this working. There are few ways really.

First thing you need to think of is where would you save that temporary content? You can save it into your database, html5 web storage, or you can even save it into a cookie, if text users are editing is not to big.

Saving temporary content into a web storage:

You can save temporary content into web storage. And then if user decides to close his browser, and come back latter, you would first look if he has some temporary content saved in his web storage. If yes then you populate his editing form with that content. You should create some kind of a tracking system to know when to save content. You could save content lets say every 5 minutes.

Saving temporary content into a database:

I assume you know enough PHP to start database connection, and enough SQL to save some content into database. What you need to do, is use AJAX to send form content into temporary database table. When user starts writing text into the form, you would every now and then send content of a form to your PHP script / action controller that would save that content into temporary table. You would be using AJAX to make this happen since you don't want to force your user to refresh browser every time you want to save temporary content. And then if user turns off his browser, and wants to edit the text, you would first check if user has some temporary content saved in database, and insert that content into his form.

There are lots of ways to make this work, you just need to decide where to save a content. I would go with the database and quick AJAX calls. Since HTML5 web storage is not supported with all browsers, and cookies are small for huge text and not designed to work this way really. And I would not rely o client side since user can easily clear his history. Database and AJAX calls are more complicated solution than HTML5 web storage, but i think that's the best way to go.

Basic idea is this:

  • Generate HTML form
  • Check if there is some temporary content and insert it into form if there is
  • Let user edit his content, and save it into a storage of your choice every 5 minutes or so.

And when dealing with HTML5 Web Storage and AJAX, i suggest you to take a look at jQuery, it can make your life easier.

You have basic idea how can you solve this, its up to you to write your code now :) Good luck!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top