MsNichols
MsNichols

Reputation: 1133

Populate Form from LocalStorage Key-ID with related Json Data

I have some json that calls back fine. The problem is, is now that I am able to set the ID in localStorage, I need to populate that record and only that record.

so my localStorage looks like:

enter image description here

Which is coming from my json and set after a button click. Now, i need to use that ID to get the related json. Here is my json, but i am not sure how to modify my ajax call to bring back that record with that ID only. Any help with this would be great.

My ajax call:

    var recID = localStorage.getItem('recordID');

var Json_return = [];
jQuery(function(){
      jQuery.getJSON('mydata.php',{},function(data){
    Json_return = data;
    console.log (data);

enter image description here

Upvotes: 0

Views: 142

Answers (1)

Manish Gautam
Manish Gautam

Reputation: 516

you can get the id from local storage by the code you used above

var recID = localStorage.getItem('recordID');

var Json_return = [];
jQuery.getJSON('mydata.php',{},function(data){
 Json_return = data;
}

and then filter the ajax call to get the data for only that id like this

var b = $.grep(Json_return, function(el, i) {
         return el.ID == recID  
       });
console.log(b);

Upvotes: 1

Related Questions