I want to create comments with AJAX, I do not know if this is the right way its my first time doing it. I have been searching google for hours and im really stuck.
What I want to achieve is to add a comment right after I click 'comment' button wihtout reloading the page.
I do not know how to create JSON array that will suit my needs.
This is my javascript:
$(function(){
var userComments = $('.userComments');
var commentsUrl="commentsLoad.php";
$.ajax({
type: 'GET',
url: commentsUrl,
dataType: "json",
success: function(komentarji){
//$.each ...
userComments.append("output...");
},
error: function(e){
console.log(e);
}
});
});
This is my PHP:
include('config.php');
$comments=array();
$commentsQuery = "SELECT * FROM komentarji";
$result = $conn->query($commentsQuery);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
$comments=array(
'id' => $row['id'],
'name' => $row['name'],
'text' => $row['text'],
'date' => $row['date'])
);
header('Content-type: application/json');
echo json_encode($comments);
}
}