0
 Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)

This is my array that i get from my print_r request it is from php it is a multidimensional array that i used json_encode now this is the data i get from success. I want to get all the value of sysid and name from this json to be put in select option how is this possible

I used this code below but i get undefined and i get 4,000 result

PAIR 0: undefined

PAIR 0: undefined

PAIR 1: undefined

PAIR 1: undefined

for (var i = 0; i < data.length; i++) {
    console.log("PAIR " + i + ": " + data[i].sysid);
    console.log("PAIR " + i + ": " + data[i].name);
}

UPDATE

my bad the first one is the print_r this is the json

[{"sysid":"1","code":"140101000","name":"China","parentid":"1"},{"sysid":"2","code":"140102000","name":"Japan","parentid":"1"},
{"sysid":"3","code":"140103000","name":"Hongkong","parentid":"1"}]

ajax is

$.ajax({
    type: 'POST',
    url: '../include/country.php',
    data: {
        id: id
    },
    success: function(data) {
        // the next thing you want to do 
        var obj = data;
        console.log(obj);
        //for (var i = 0; i < data.length; i++) {
        //console.log("PAIR " + i + ": " + data[i].sysid);
        //console.log("PAIR " + i + ": " + data[i].name);
        //}


    }
});
6
  • show us the ajax request code, please. Commented Jan 17, 2015 at 4:01
  • your code should work.. i am not seeing any errors.. Commented Jan 17, 2015 at 4:07
  • in my console.log i get Pair 0: undefined how is that ok sir @MrBearAndBeer but in regular console log if the console log is only console.log(data); i get result what i want to show in console log is only the sysid and name Commented Jan 17, 2015 at 4:10
  • you should use the obj.length, and not the data.length Commented Jan 17, 2015 at 4:14
  • Am I right that you want to put the results into a select input? That's what I feel like you're asking Commented Jan 17, 2015 at 5:46

1 Answer 1

1
$.ajax({
    type: 'GET',
    url: '../include/country.php',
    dataType : "json",
    data: {
        id: id
    },
    success: function(data) {
       for(var i = 0; i < data.length; i++) {
           console.log("PAIR " + i + ": " + data[i].sysid);
           console.log("PAIR " + i + ": " + data[i].name);
       }
    }
});
Sign up to request clarification or add additional context in comments.

10 Comments

I'm guessing the OP is showing us the PHP code, not the returned JSON.
why? just delete the var obj = data, or update your loop to use obj instead data.
i did try to just use the json alone i mean i copied it it worked but when i use data it is undefined what seems to be the problem?
also i noticed that the first item it has no json equivalent it has a result for print_r but for json none it is blank..
why u are using the POST method, if you want GET the data, and not pass?
|

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.