我有commentdb.php文件,其中创建了类.like-btn
的按钮,并已链接了script.js文件
并拥有另一个使用isset($_POST['key'])
的db.php文件。
script.js:
$(document).ready(function(){
$('.like-btn').click(function(){
var post_id = $(this).data('id');
alert(post_id);
$clicked_btn = $(this);
var act = "";
if ($clicked_btn.hasClass('fa-thumbs-o-up')) {
act = 'like';
} else if ($clicked_btn.hasClass('fa-thumbs-up')) {
act = 'unlike';
}
// console.log(act);
$.ajax({
type: "POST",
url: 'db.php',
data: {
'key': 1,
'action': act,
'post_id': post_id
},
success: function(response) {
console.log(1);
// res = JSON.parse(data);
alert(act);
if (act == 'like') {
$clicked_btn.removeClass('fa-thumbs-o-up');
$clicked_btn.addClass('fa-thumbs-up');
} else if (act == 'unlike') {
$clicked_btn.removeClass('fa-thumbs-up');
$clicked_btn.addClass('fa-thumbs-o-up');
}
// alert(res.likes);
}
});
});
});
db.php:
<?php
session_start();
// $email = "$_SESSION['email']";
$conn = mysqli_connect('localhost','root','','commentsection');
echo "working?";
// $user_id = 1;
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
if (isset($_POST['key'])) {
echo "isset working?";
$post_id = $_POST['post_id'];
$action = $_POST['action'];
switch ($action) {
case 'like':
console.log($email);
$sql2 = "INSERT INTO rating (uid, rating, user_id) VALUES ($post_id, 'like', $email) ON DUPLICATE KEY UPDATE rating='like'";
break;
case 'dislike':
$sql2 = "INSERT INTO rating (uid, rating, user_id) VALUES ($post_id, 'dislike', $email ON DUPLICATE KEY UPDATE rating='dislike'";
break;
case 'unlike':
$sql2 = "DELETE FROM rating WHERE uid=$post_id AND r_uid=0";
break;
case 'undislike':
$sql2 = "DELETE FROM rating WHERE uid=$post_id AND r_uid=0";
break;
default:
break;
}
mysqli_query($conn, $sql2);
echo getRating($post_id);
// exit(0);
}
?>
我尝试过使用isset($_POST['key'])
,然后使用isset($_POST['action'])
和isset($_POST['post_id'])
,但是它不起作用,该函数未执行。该条件可能会返回false。
我做了大部分在Google上发现的事情,但没有一个起作用。 我真的对此并不了解。因此,请好帮我,坚持一个星期。