1

So i´m, trying send data from php to js.

PHP

$balkTypes[] = $stmt->fetchAll();
echo json_encode($balkTypes);

JS

balkTypesData = {}; //Outside Ajaxcall

success: function(result){
    balkTypesData = result;
    Console.log(balkTypesData);
}

Console

[[{"id":"3","typ":"Bas 200*600","hojd":"200","bredd":"600","rec":"","viktM":"135"},{"id":"2","typ":"Bas 240*600","hojd":"240","bredd":"600","rec":"","viktM":"160"},{"id":"5","typ":"Isol\u00e4tt 240*600","hojd":"240","bredd":"600","rec":"","viktM":"105"},{"id":"4","typ":"Kontur 240*600","hojd":"240","bredd":"600","rec":"","viktM":"105"},{"id":"6","typ":"Passbit","hojd":"0","bredd":"0","rec":"","viktM":"0"}]]

Now, i´d like to search my Json object?!
I´d like to find "viktM" for "typ:Bas 200*600"

//Get balkType weight/m
var searchField = "typ";
var searchVal = "Bas 200*600";
for (var i=0 ; i < balkTypesData.length ; i++){
   if (balkTypesData[i][searchField] == searchVal) {
     weigth = balkTypesData[i]['viktM'];
     console.log(weigth);
   }
}

First of all, it seams that i cannot use .lengton "balkTypsData". it gives me 410 hits. Must be all characters?

Second, i cannot find how to access part of my object.
If i use: console.log(balkTypesData[i][searchField]);
I get: "Undefined"
I have also tried to remove the "[i].

So what am i missing?
Be gentle i´m still learning.

1

4 Answers 4

5

Take a look at $.parseJSON() (jQuery) or JSON.parse() (vanilla):

With jQuery

success: function(result){
    balkTypesData = $.parseJSON(result);
    console.log(balkTypesData);
    console.log(balkTypesData[i][searchField]);
}

Without jQuery

success: function(result){
    balkTypesData = JSON.parse(result);
    console.log(balkTypesData);
    console.log(balkTypesData[i][searchField]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, Thank you. Works very well!
1

When you receive the data from your AJAX request it's not JSON, just a string.

The length result that you're getting is the length of the string, not the amount of elements within the array.

Furthermore you're setting $balkTypes[] which means that you're trying to add 1 entry in the array of $balkTypes however $stmt->fetchAll(); also returns an array so you now have a nested array which is not needed.

In your PHP file change

$balkTypes[] = $stmt->fetchAll()

to

$balkTypes = $stmt->fetchAll()

this will make sure that when you fetch your data it will be an array containing all objects instead of an array containing the array of objects.

Then in your JS, instead of trying to directly read from the string, use JSON.parse() to convert the json string into a collection of JS objects/integers/arrays/strings/booleans

e.g.

success: function(result) {
    balkTypesData = JSON.parse(result);
    console.log(balkTypesData);
}

EDIT

As pointed out by Armen you could also set the dataType: 'json' in the AJAX request, when the AJAX request returns it will automatically do the JSON.parse() so you can just directly console.log(result); to see the output.

Within the console.log you should now see the nested structure instead of just the string.

From here on your loop which checks the values seems correct and I would not change it unless it tells you that something is wrong.

Docs: JSON.parse();

1 Comment

Very well explained. Thank you so much!
1

Set in your jQuery $.ajax request additional attribute dataType: 'json'

$.ajax({
  type: "POST",
  dataType: "json",
  url: url,
  data: { params },
  success: function( response ) 
  { 
     // Your data will be already json no need to parse it
     console.log(response);
  }
});

2 Comments

oh, so i can just add "Datatype" on my ajaxcall... cool. Thank you.
Yes exactly dataType: "json" will consider that return information is json and you will have in result already json look here - api.jquery.com/jquery.ajax
0

You are encoding a JSON on the PHP side. You are not decoding it on the JS side.

You should look at JSON.parse()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.