Reputation: 593
I have this code:
function openIzmeni(value) {
$.ajax({
url: "getRzaizmenu.php",
type: "POST",
async: true,
data: { vrednostid:value}, //your form data to post goes here as a json object
dataType: "html",
success: function(data) {
console.log(data);
$('#brojracuna1').val(data[0].br);
console.log(data[0].br);
console.log(data.br);
so as you can see I call getRzaizmenu.php to get JSON output. getRzaizmenu.php code is:
try {
$result = $db->prepare("SELECT * FROM racuni WHERE ID=:id AND user_id=:user_id");
$result->execute(array(':id' => $_POST['vrednostid'], ':user_id' => $user_id));
$result = $result->fetchAll();
$r= json_encode($result);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $r;
and this php return me output:
[{"ID":"22","0":"22","prefiks":"","1":"","br":"14321","2":"14321","sufiks":"993.67","3":"993.67","kupac":"Pavle Aleksov","4":"Pavle Aleksov","adresa":"Desanka Maksimovic 6\/a","5":"Desanka Maksimovic 6\/a","grad":"18320 Dimitrovgrad","6":"18320 Dimitrovgrad","pib":"567890","7":"567890","total":"1200.00","8":"1200.00","valuta":"Din","9":"Din","nacin":"gotovinsko","10":"gotovinsko","datum":"2015-05-25","11":"2015-05-25","rok":"2015-05-25","12":"2015-05-25","isporuka":"2015-05-25","13":"2015-05-25","naplaceno":"0000-00-00","14":"0000-00-00","napomene":"","15":"","interne":"","16":"","jezik":"","17":"","status":"","18":"","sifra":"yyx5y","19":"yyx5y","user_id":"1","20":"1"}]
So this code return me "0":22
and "ID":22
but its the same column.
and you can see I try to use this data into ajax success function like this:
success: function(data) {
console.log(data);
$('#brojracuna1').val(data[0].br);
console.log(data[0].br);
console.log(data.br);
but I get undefined.
How I can use data
in success?
Upvotes: 0
Views: 78
Reputation: 3859
you have encoded the data, that's ok. But while parsing you are not decoding the data. So you have to decode the data using JSON.parse() and parse function.
Upvotes: 1
Reputation: 11830
You need to parse the JSON, try like this
success: function(data) {
$.each(data, function(index, element) {
console.log(index);
console.log(element);
});
});
Upvotes: 0