I’m trying to make a like system. Everything works well without Ajax but with ajax is not ready.
I mean, The ajax post is ok, the answer is ok, but the output on the screen is not ok. When i click the LIKE button, noting happens (no page refresh). When i refresh the post, there is one more like. `
without ajax, the functions and everything is working
Here is my Ajax file:
<?php
ini_set('display_errors', 1);
include_once ('../classes/comments.class.php');
$key = $_POST['key'];
$sessie = $_POST['login'];
$like1 = new Comments();
$like1 -> Key = $key;
$like1 -> User = $sessie;
try{
$feedback["status"] = "succes";
$like1 -> addlikes();
$feedback["message"] = $like1 -> CountLikes();
} catch (Exception $e) {
$feedback["message"] = $e -> getMessage();
$feedback["status"] = "error";
}
header('Content-type: application/json');
echo json_encode($feedback);
?>
and here the code in my PHP page:
<script type="text/javascript">
$(document).ready(function() {
$("#knop").click(function() {
var login = $("#login").text();
var key = $("#key").text();
$.ajax({
type: "POST",
url: "assets/ajax/check_likes.php",
data: { login : login , key : key},
success: function( msg ){
if ( msg.status == "success" ) {
$("#likes h1").text( msg.message );
}
}
});
return false;
});
});
</script>
Can you try adding an
exit();afterecho json_encode($feedback);in ajax file?Edit: Also try using the console in Firebug (or chrome inspector or whatever) to check out if the ajax response is valid json.