我最近一直在关注phpacademy,但是它们的代码根本无法正常工作。它可以很好地显示文章,但是like按钮不起作用,它停在'$ .post'方法上,其工作是将article_likes更新为“ 22”,请看: index.php
<html>
<head>
<?php include 'core/db/connect.php'; ?>
</head>
<body>
<?php
$articles = get_articles();
if(count($articles) == 0)
echo "No art";
else{
//Displaying articles.
foreach($articles as $article){
echo $article['article_title']."<br>";
echo "<a onclick=\"like_add(".$article['article_id'].");\" href='#'>Like</a><br>";
echo "<span id='article_".$article['article_id']."_likes'>".$article['article_likes']. "</span>Likes<br> ";
}
}
?>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/like.js"></script>
</body>
</html>
like.js
function like_add(article_id){
$.post('like_add.php',{article_id:article_id},function(data){
if(data[0] == 'success'){
like_get(article_id);
} else {
alert(data[0]);
}
});
}
function like_get(article_id){
$.post('like_get.php',{article_id:article_id},function(data){
$('#article_'+article_id+'_likes').text(data[0]);
});
}
like_add.php
<?php echo "success";?>
like_get.php
<?php echo "22";?>