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?
json_encodeon country.php and share the output with questionprint_rand yourjson, please add both in the question. so it would be easy for others to identify the problem