I start the process:

function vhod() { $.get( "http://blabla.com/vhod.php", {..........} ) .done(function( data ) { localStorage.setItem(data, value) }); } 

On the server ( vhod.php ):

  $result = mysqli_query($con,"SELECT k_ls, v_ls FROM users"); while($row = mysqli_fetch_array($result)) { echo ($row['k_ls']); } 

As a result, I only get the value of the k_ls column, and I would like to get all the values ​​- both k_ls and v_ls for successful saving. In the Mysql table, the values ​​of the k_ls and v_ls can be several.

  • Have you heard about json ? in what form you are going to save these values ​​in general, you should now have one line coming together that combines the k_ls values ​​of all sample lines without any separators. How do you plan to use it? - teran
  • I heard, for my case I do not know how to apply ... - Nik
  • so you that with the obtained data want to do that on a total? now you have one line from k_ls . - teran
  • localStorage.setItem (data, value) is what I want. I understand that one line, how to make that not one was? - Nik

1 answer 1

I do not know if you need it, but you can try to do this:
On the server side, generate the necessary data in JSON format, sending the corresponding headers (note the fetch function is different) :

 $result = mysqli_query($con,"SELECT k_ls, v_ls FROM users"); $data = []; while($row = mysqli_fetch_assoc($result)) { //сохранит имена полей $data[] = $row; // добавить к результату } header("Content-type:application/json"); //отправить заголовок о типе контента echo json_encode($data); // и сами данные 

on the client side, after receiving the data, you can write them in a string and in localStorage

 $.get( "http://blabla.com/vhod.php", { ... } ) .done(function( data ) { // получили JSON данные localStorage.setItem('myKey', JSON.stringify(data)); }); 

further, when needed, load data from localStorage

 var strData = localStorage.getItem('myKey'); var data = JSON.parse(strData); 

and process them

 $.each(data, function(idx, v) { console.log( idx +': ' + v.k_ls + '/' + v.v_ls); }); 
  • And how can you line up: localStorage.setItem ('myKey', JSON.stringify (data)); replace with localStorage.setItem (value k_ls, value v_ls) ;? - Nik
  • Now, after JSON.stringify (data) I have: [{"k_ls": "22", "v_ls": "11"}, {"k_ls": "33", "v_ls": "44"}], But I need to save this: localStorage.setItem ('22 ',' 11 '); localStorage.setItem ('33 ',' 44 '); - Nik
  • one
    as for me, it is better then to store the option { "22": 11, "33": 44} , they are at least localized from other possible data. it will be more correct. For this, $data[] = $row replace with $data[$row['k_ls']] = $row['v_ls'] ; - teran
  • but if you need as you wrote, then in .done should be $.each(data, function(idx, v){ localStorage.setItem(v.k_ls, v.v_ls); } - teran