最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Is it possible to pull data from HTML5's local storage and save to server database? - Stack Overflow

programmeradmin3浏览0评论

I have an idea for a social web app and part of the idea is to pull data from localstorage onto the server database. The reason for this is say you have an idea and you want to put it on your next status for this social app, but you have no signal and wireless anywhere. You write out the status on the app and it saves it to the localstorage for use when they get back to somewhere where they can connect their phone online.

They then get to a place where they can go online, the app then detects that there is a change in the localstorage and then pulls it from there and saves to the server database.

Is this at all possible, if so how would you go about implementing it? If it's not at all possible, do you think this could be sometime in the future?

I look forward to hearing from your ments and answers on this one as I think it could be quite a big discussion.

I have an idea for a social web app and part of the idea is to pull data from localstorage onto the server database. The reason for this is say you have an idea and you want to put it on your next status for this social app, but you have no signal and wireless anywhere. You write out the status on the app and it saves it to the localstorage for use when they get back to somewhere where they can connect their phone online.

They then get to a place where they can go online, the app then detects that there is a change in the localstorage and then pulls it from there and saves to the server database.

Is this at all possible, if so how would you go about implementing it? If it's not at all possible, do you think this could be sometime in the future?

I look forward to hearing from your ments and answers on this one as I think it could be quite a big discussion.

Share Improve this question edited Jul 12, 2013 at 10:23 MrCode 64.6k10 gold badges92 silver badges113 bronze badges asked Jul 12, 2013 at 10:09 CheckeredMichaelCheckeredMichael 5718 silver badges30 bronze badges 1
  • Yes, it is possible. This is exactly how many webapps use localstorage (e.g. offline Gmail). – eggyal Commented Jul 12, 2013 at 10:21
Add a ment  | 

2 Answers 2

Reset to default 3

Yes it's possible, why wouldn't it be? When you pull data from the local storage, the data works just like any other Javascript variable, there are no restrictions that would stop you sending it to a server other than the lack of Internet connection.

how would you go about implementing it?

First you'd need to detect the connection status, see this question. So when a user tries to update their status, it checks if the connection is online and if it isn't then save it to local storage.

Use the methods in the linked question to detect when the connection es back up, and at that point pull the data from local storage and send it to the server via ajax, then clear the local storage to prevent duplicate data being sent.

You can periodically check navigator.onLine in your Javascript to get the online status of a device. Note that while

  navigator.onLine == false 

safely tells you that a device of offline,

 navigator.onLine == true 

doesn't necessarily mean it has access to the internet and can perform the request, just that it is connected to some sort of network.

Once you assume that the device is not offline you'd send an ajax request (I remend using jQuery's $.ajax function) in which you POST the data from your localStorage

(localStorage.getItem('dataToPull'))

to a PHP script, which then inserts it into your MySQL database.

If that ajax request fails, and you're sure it's not getting a PHP/MySQL error, it's safe to assume that although the device is connected to a network and navigator.onLine is true, there's no internet connectivity. You can then do some error handling, e.g. poll the device again in 5 minutes.

发布评论

评论列表(0)

  1. 暂无评论