0
if (isset($_POST['continentid'])) {
      $stmt = $dbh->prepare("SELECT * FROM country_tbl WHERE parent_id = ? ");
      $stmt->bindValue(1, $_POST['continentid'], PDO::PARAM_STR);
    if ($stmt->execute()) {
        if ($stmt->rowCount() > 0) {
            while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
            } 
            //print_r($country);
            echo json_encode($country);
            //echo "312321321321";
            //return $country;

        }
    }
}


$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: 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);
       }
    }
});

I have this code above that send request using jquery ajax it will return an id that will be used as parameter for select statement. I then use these values on select option box. I have posted a question for this here. It is working ok now the odd thing is the first value on continent does not give the list of country if using json but when i use print_r it is giving me the list of countries but for the other continent json value is ok i am getting value for country. Question is why does the first value on the list does not give json value but if print_r it has value what is wrong in this setting?

Update

If i do

print_r($country); echo json_encode($country);

for first element

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
        )
)

If i do

print_r($country); echo json_encode($country);

for second element

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
        )
)


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

UPDATE

I think i found the problem although i havent found a solution yet i think the character ñ and ' is the reason why they wont return a value for json any idea on how to make it return these values?

5
  • share your json structure please. Commented Jan 20, 2015 at 5:29
  • something like above...for example that one is list of continent the first one wont return value but if print_r i will return value Commented Jan 20, 2015 at 5:31
  • use json_encode on country.php and share the output with question Commented Jan 20, 2015 at 5:33
  • if there is a diff between print_r and your json, please add both in the question. so it would be easy for others to identify the problem Commented Jan 20, 2015 at 5:34
  • the difference is when i do print_r i get an output in ajax success but on json none Commented Jan 20, 2015 at 5:48

4 Answers 4

0

try it. JSON.parse(data) convert string to json.

    $.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + data[key].sysid);
           console.log("PAIR " + i + ": " + data[key].name);
       }
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

use (each loop ) function instead of ( for loop)
sorry i didnt see that
data.each(key,value) { console.log("PAIR " + i + ": " + data[key].sysid); console.log("PAIR " + i + ": " + data[key].name); } use this instead of (forloop )
data.each(key,value) { unexpected token { when i remove it i get unexpected token A
0
$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + value.sysid);
           console.log("PAIR " + i + ": " + value.name);
       }
    }
});

Comments

0

Try this,

success: function(data) {
   $(data).each(function(i,v){
       console.log("PAIR " + i + ": " + v.sysid);
       console.log("PAIR " + i + ": " + v.name);
   });
}

var data = [{
  "sysid": "1",
  "code": "140101000",
  "name": "China",
  "parentid": "1"
}, {
  "sysid": "2",
  "code": "140102000",
  "name": "Japan",
  "parentid": "1"
}, {
  "sysid": "3",
  "code": "140103000",
  "name": "Hongkong",
  "parentid": "1"
}];
$(data).each(function(i, v) {
  console.log("PAIR " + i + ": " + v.sysid);
  console.log("PAIR " + i + ": " + v.name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Change the piece of php code like,

if ($stmt->rowCount() > 0) {
     $country=array();// initialise $country first
     while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
     } 
     echo json_encode($country);
 }

6 Comments

same result sir no console.log for json but there is for print_r for first element but on second element all is there
Remove the print_r($country) from your php code. Only there should be echo json_encode(); As you are using dataType : "json",. And the response must be in json format
i remove print_r same thing no console.log for first for second there is
do i need to change other as i can see you only add initialize right?i add initialize then your code same thing why is this happening?
@HogRider Can you show me your online demo for the same. If not, then it seems difficult to point out the problem.
|
0

Adding this line $dbh->query("SET CHARACTER SET utf8"); to my config file fixed the problem so it really has something to do with special characters like ñ json_encode with return null value if there are special characters

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.