I have a vote system that is similar tho youtube vote system.once I sent the relevant data to the php function i want to get back the query results back to the ajax function.I have array of likes and dislikes .when I encoded the array ajax success function returns as follows. {"likes":2,"unlikes":0} I want to get likes and unlike values(2 and 1) to display in the html page. how can I get them.
user-likes.js
function liked(answerId) {
var anID = answerId;
var clickedIcon = $('#Like');
//var nextIcon=$('#like')
$.ajax({
dataType:'json',
url: 'vote.php' ,
type: 'post',
data: {
'liked': 1,
'postid': anID
},
success: function (results) {
alert(results);
clickedIcon.removeClass();
clickedIcon.addClass('mdi-action-thumb-up teal-text');
//.text(res[0].likes);
},
error: function () {
}
});
}
vote.php
<? php include '../../backend/connection.php';
session_start();
$username = $_SESSION['username'];
if (isset($_POST['liked'])) {
$answerId = $_POST['postid'];
$result = mysqli_query($connection, "SELECT * FROM answervote WHERE aID='" . $answerId . "'");
$row = mysqli_fetch_array($result);
$nUp =(int)($row['voteUpCount'])+1 ;
$nDown=(int)$row['voteDownCount'];
$law_query = mysqli_query($connection, "SELECT aUser FROM answer WHERE aID='" . $answerId . "'");
$law = mysqli_fetch_array($law_query);
$lawyer_name=$law['aUser'];
$point_query=mysqli_query($connection,"SELECT points FROM lawyer WHERE username='".$lawyer_name."'");
$point_result=mysqli_fetch_array($point_query);
$points=$point_result['points']+1;
try{
mysqli_autocommit($connection, FALSE);
mysqli_query($connection,"UPDATE lawyer SET points='$points' WHERE username='".$lawyer_name."'");
mysqli_query($connection, "INSERT INTO likes(username,aID) VALUES ('$username','$answerId')");
mysqli_query($connection, "UPDATE answervote SET voteUpCount='$nUp' WHERE aID ='" . $answerId . "'");
mysqli_commit($connection);
}catch(Exception $e ){
$connection->rollback();
}
$ratings=[
"likes" => $nUp,
"unlikes"=> $nDown
];
echo json_encode($ratings);
}
returned result from success function:{"likes":2,"unlikes":0}