Here I have sme .ajax function that work well:
$( document ).ready(function() {
$('a#kom').click(function (e) {
var tabela = 'parcele';
$.ajax({
url: "getComments.php",
type: "POST",
async: true,
data: { ajdi:ajdi,tabela:tabela}, //your form data to post goes here as a json object
dataType: "html",
success: function(data) {
console.log(data);
},
error:function(data) {
console.log(data);
}
});
});
});
and this function produce this JSON:
[{"datum":"2014-05-22","komentar":"Testiranje za komentare da vidimo kako moze da sluzi, dali radidobro, bla bla bla bla bla bla bla bla bla bla bla bla ... ..."}]
Iknow its a basic question but I cant find any good resource... How I can into .ajax function when function is success to create this html:
'<div class="notes">
<h4>Tekst zaglavlje</h4>
<h5>'+datum+'</h5>
<p>'+komentar+'</p>
</div>'
for every object I get from JSON... so like that:
success: function(data) {
// CREATE A HTML FROM JSON DATA FOR EACH JSON OBJECT. HOW?
console.log(data);
},
UPDATE WITH PHP
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
try {
$result = $db->prepare("SELECT * FROM komentari WHERE id_utabeli=:ajdi AND tabela=:tabela");
$result->execute(array(':ajdi' => $_POST['ajdi'], ':tabela' => $_POST['tabela']));
$result = $result->fetchAll();
$temp = array();
foreach($result as $r) {
$temp[] = array('datum' => (string) $r['datum'], 'komentar' => (string) $r['komentar']);
}
$table = $temp;
$jsonTable = json_encode($table);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;
}
else {
echo 'ERROR 404';
}