localStorage to MySQL - javascript

Is there any easy way to move data from localStorage or sessionStorage into a MySQL database with callbacks?
I'm guessing this would be done with AJAX, but I can't find a good how-to or guide for it. Basically, I'd like to send localStorage to the server, and alert the user as to whether or not the data was successfully stored by the server into MySQL.

It's been a while since I messed around with this, but localStorage is basically just an object, right? Which means that it could be sent as JSON? So you could construct an AJAX call to a PHP page, send the JSON, and then (after the PHP has messed with it and tried to insert it and echoed the result) alert the user as to what happened, right?
(Obviously, you could do this with any server-side language.)

Related

Reload data on change

so I will try to be as clear as possible on my question!
On my site I have a table with the latest reservations, it's a fetch request that retrieves the data in PHP in JSON format and Javascript that puts them in shape, it works very well. But now I would like to go one step further, and make an automatic system, which would only have the Javascript function to retrieve and format the data. But I would need to do this without having to do a setInterval() or wait for the user's click, somehow only when a row is added (which is not done by the admin but the system the detect), the array is reloaded. I don't know at all if it's possible or not, and that's why I'm asking here?
(I only work in Javascript with Jquery and with PHP, I don't know if I need another lib?)
Thanks in advance and have a nice day!
I think if you use websocket, you can get better system.
but, if you can't, I recommend you use setInterval() to check if the row added.
You can install websocket server with nodejs easily.
and you have to add send event from php to nodejs ( http ) when new row added in your PHP server.
and add javascript websocket client.

Saving and retrieving data on server with Javascript & PHP

I have a form on my website that I want to ensure each client only submits once per year.
To do this, I would like to save the client's id # and the current year to a .txt file on my server when they submit the form.
When the form is submitted, I also need to check the current contents of that file to ensure their id has not already been recorded, and display a message if they have already submitted the form that year.
I believe I need to use PHP to do this, but I'm brand new to PHP and I'm also not very experienced with jQuery. Any assistance would be much appreciated!
Using a simple database would be much better, because it will be easier to retrieve the stored data later on. There are more than enough tutorials if youre not familiar with databases (use mysql database if you have few experience with databases).
If you use a database you can also store the submitted data from the form very easy.
As Dan already said, first learn PHP and database connects and querys and then jQuery (which you dont need for putting data from a form into a database, but with ajax its cooler (you dont even need jQuery for ajax, vanilla javascript can also do that for you) ;) ).
Just google everything, there are more than enough tutorials online.
If you don't want to use a database, probably the simplest option will be to rename the received file with clientId-currentYear when you put it on the server.
For example, when a user submits a file for client 12345, you will have to check if a file 12345-2018 already exist. If it doesn't exist, you can create it, if not, you send your message to the user.
This is going to be easier to manage than a text file or a database if your programming level is low.

Passing data to server using javascript securelly?

At the moment I'm passing data to node js server via javascript and the reason i must do this is because I'm using socket.io so I emit the data from the client.
There's a method on javascript to store data on a div and the data is invisible on the div $('#divID').data('name','value'); but still the user can see the data when i'm storing it and when I'm sending it. Is there a way I can store and emit the data via javascript the way that user can't change or see it?
It's a 16-digit user ID.
Javascript in essence is meant to be publicly viewable. Anybody with the knowledge of Javascript will be able to see everything that you are doing. So, you will have to continue to send it to the server before it can go to the database.

How can I pass data from one page to another in PHP and Javascript without from?

How can I pass data from one page to another page in PHP using Javascript without from?
In my blog have a post comment with email Id
and this is routing using foreach loop
problem occur when get email id and comment
how can i do this? plz help
you can make use of client side cookie for this. one page you create cookie and on second page read that cookie.
I guess that what you dislike with forms is the page reloading on the client side.
If that is the case then you can use ajax requests (maybe with the help of a framework like jQuery, or pure javascript with the xmlHTTPRequest()).
Alternatively you can use cookies, but it's not what they really are for.
If you don't mind the page reloading, you can also use HTTP GET requests with javascript :
location.assign("http://www.my_site.com/index.php?param1=value1&param2=value2");
You can also store data in Web storage if the data doesn't necessarily need to be processed in PHP.
You will need to have a backup system in cookies though for the older browsers that do not support his.

Storing data locally in browser and sending to server

I have read about HTML Filesystem and LocalStorage but I'm not sure what is the best way to implement what I need, if it is all possible.
Basically what I need is for the browser to store some data, preferably XML format, and then send this to server. My ideal implementation is writing to a text file and then uploading this file to the server. But I understand this is not possible to do locally in browsers.
Also I read that Filesystem is not being supported anymore. So is Localstorage the only option to store information in client side?
The biggest problem I would have with Localstorage is that I might have multiple pages, but I need all of them to write to one place.
For example let say I have a web app and the user starts with page A, then navigates to page B and then page C and so on. I need to store some information on each page, and then finally, say page D, I want to send all the previously stored data to the server.
So firstly, am I trying to do something impossible?
If not, what is the best way to go about this?
Thank you.
To simply answer your question, yes, you can achieve that. You can store anything in key value pairs in the local storage from different pages and you can send it to the server whenever ready. You can also look at session based storage if you want it to be saved only for a session.

Categories