PHP and Javascript JSON.stringify in localstorage and convert it into JS object? -


lets have simple php echo's/prints this:

[['staff', 'somewhere', 'map-marker-icon.png'], ['chinese', 'london', 'map-marker-icon.png'], ['trade', 'essex', 'map-marker-icon.png'], ['fare', 'london', 'map-marker-icon.png'], ] 

i whatever php echo's on html page using ajax , stores them in localstorage using stringify.json so:

$.ajax({ type: "post", url: "my-php-page.php", data: datastring, crossdomain: true, cache: false, beforesend: function(){}, success: function(data){  localstorage.setitem('mapmarkers', json.stringify(data)); } }); 

i ajax response (data) stored in localstorage , can see there being stored.

now, next step i'm trying same localstorage value , use javascript object.

so went ahead , did in html page after localstorage set above:

var locations = json.parse(localstorage.getitem('mapmarkers')); alert(locations); 

the issue i'm having when alert(locations); null in alert box!

but can see localstorage being stored in browser!

this see in localstorage:

value:

"\r\n[['staff', 'somewhere', 'map-marker-icon.png'], ['chinese', 'london', 'map-marker-icon.png'], ['trade', 'essex', 'map-marker-icon.png'], ['fare', 'london', 'map-marker-icon.png'], ] 

json

    0         "     "      1         "\n"      2         "["      3         "["      4         "'"      5         "s"      6         "t"      7         "a"  etc etc......          "]" 

could please advise on issue have been spending on 2 days trying figure out no luck @ all.

any appreciated.

edit

okay, seems drawing board keep updating question make nice tutorial others too:

okay, tried change php this:

$return_arr = array(); $sql = "select * restaurants"; if ($result = mysqli_query($db, $sql )){     while ($row = mysqli_fetch_assoc($result)) {     $row_array['id'] = $row['id'];     $row_array['location'] = $row['location'];     $row_array['name'] = $row['name'];     $row_array['type'] = $row['type'];      array_push($return_arr,$row_array);    }  }  mysqli_close($db);  echo json_encode($return_arr); 

this prints this:

[{"id":"1","location":"fitzrovia","name":"staff welfare","type":"indian"},{"id":"2","location":"soho, london","name":"perfect chinese","type":"chinese"},{"id":"3","location":"southend-on-sea, essex","name":"fare trade","type":"kebab"},{"id":"5","location":"soho, london","name":"fare trade","type":"kebab"}] 

the first thing noticed {} instead of []....

mainly because need javascript object this:

   [['staff', 'somewhere', 'map-marker-icon.png'], ['chinese', 'london', 'map-marker-icon.png'], ['trade', 'essex', 'map-marker-icon.png'], ['fare', 'london', 'map-marker-icon.png'], ] 

withdout "id":...,"location":.. etc etc.....

i don't know if use {} in javascript! so, advise on grand.

the second thing did changed ajax code in html this:

    $.ajax({     type: "post",     url: "my-php-page.php",     data: datastring,     crossdomain: true,     cache: false,     beforesend: function(){},     success: function(data){      localstorage.setitem('mapmarkers', json.stringify(data)); var locations = json.parse(localstorage.getitem('mapmarkers'));  alert(locations);      }     }); 

this in alert box inside ajax success:

[{"id":"1","location":"fitzrovia","name":"staff welfare","type":"indian"},{"id":"2","location":"soho, london","name":"perfect chinese","type":"chinese"},{"id":"3","location":"southend-on-sea, essex","name":"fare trade","type":"kebab"},{"id":"5","location":"soho, london","name":"fare trade","type":"kebab"}] 

i don't think that's correct since not json.parse(...); string!

any advise on how proceed here appreciated.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -