1

hope you're having a good day how can i insert the local storage value into the database, here's the code i'm using

function fun() {
  var count = localStorage.getItem("count") || 0;
  localStorage.setItem("count", ++count);
}

function result() {
  document.write("your mark is " + (localStorage.getItem("count") || 0) + " from 9")
  }

function clear(){
    window.localStorage.clear();
}  

i want to insert the value of the variable count into the database .. is it possible ?

Dee994
  • 55
  • 1
  • 8
  • Create a service that writes it to the db using rest and then call that service from javascript passing over localStorage.getItem("count") as one of the parameters. I'm sure there is an easier way though. – James K Mar 05 '16 at 07:25
  • [HTML5 localStorage DB usage alike facilitator with JSON format](https://github.com/joaoN1x/DBosta.js) – munsifali Mar 05 '16 at 07:29
  • **Yes** it is possible! – Vikas Nokhwal Mar 05 '16 at 07:29
  • @VikasNokhwal sorry bear with me for a bit because i'm so new with this .. can i return the value of count into a submit button and then just insert it to the database like any other submit info button ?! would it work ? – Dee994 Mar 05 '16 at 16:16
  • Yes you can pass count with query string – Vikas Nokhwal Mar 05 '16 at 16:32
  • @VikasNokhwal so i can just make `function result() { return (localStorage.getItem("count") || 0) }` and then put the result function onclick on the submit button and then submit it to the database ? – Dee994 Mar 05 '16 at 16:35

1 Answers1

0

Here's your answer - it's an explanation in more detail of how to get data from javascript to your database using a post or get to an endpoint that you have created which will write the value of localStorage into your database.

The key point is that you need to use a client-side call to make a server write, and this is typically done using the methods described in the below answer. If not, it should help you search more effectively by finding different methods related to this one, that might suit you.

How to pass data from Javascript to PHP and vice versa?

This code might be helpful to explain the idea:

function result() {
  var resultText = "your mark is " + (localStorage.getItem("count") || 0) + " from 9";
  document.write(resultText);
  // set the full text into the hidden field value, or just do .val(localStorage.getItem("count")) if you only want the count.
  $("#hdnFieldName").val(resultText);
  }

  <input type="hidden" id="hdnFieldName" name="hdnFieldName" value="" />
  <input type="submit" text="Submit button" onclick="result();" />
Community
  • 1
  • 1
James K
  • 909
  • 10
  • 26
  • so can't i just make the result function like this ` function result() { return (localStorage.getItem("count") || 0) }` and then put the function on a submit button and just submit it to the database ? won't that work ? – Dee994 Mar 05 '16 at 16:52
  • If you have a page that is already getting submitted and fields are being written to the database then you could create a hidden field and set the value of the hidden field upon submit and use that in your post back to write to the database. – James K Mar 05 '16 at 16:56
  • i can set the function result in the value of the hidden field ,, then it'll do the function and get the value ? is that what you mean ? – Dee994 Mar 05 '16 at 17:26
  • Yes, basically right. Something needs to get the function result into the value of the hidden field for post back - so the event that does it could be the page submit which runs the function, and the function can set the value of the hidden field. So the submit button onclick event would run the function and then continue with page submit. The function would put the value into the hidden field using JavaScript or jquery. I can write the code to show you exactly what I mean if you don't come right. – James K Mar 05 '16 at 17:32
  • can you do that please ? cuz i don't really get it .. note that the submit form is in a different page than the localstorage functions .. i'm trying to count a result for questions on a different pages and the local storage is the only way .. now on the last question there's a submit button that will submit the result to the database .. so i thought of calling the function .. but i don't know so yes please can you write it ? i'll be really thankful – Dee994 Mar 05 '16 at 17:40
  • I pasted in the code explanation in my answer above - will you please let me know if it's helpful and you can get it to work, or if you need more help. If you need more help then maybe you should put more of your solution/problem here so I can see how you've done it. – James K Mar 06 '16 at 09:10
  • yes sir .. i'm working on my database right now .. i wanted to try and look for things to have the bigger picture ! once i get to it i'll let you know .. thank you very much ! – Dee994 Mar 06 '16 at 20:13