PHP中AJAX请求触发无关控制器方法的问题排查
问题:点赞按钮点击后报错指向原本正常运行的show()方法
我正在实现点赞按钮的更新功能,已在Posts控制器中创建likepost()方法,同时编写了对应的Model、View及AJAX代码。但点击按钮时控制台出现报错,错误指向原本运行正常的show()方法,相关代码如下:
控制器likepost()代码
public function likepost() { if (IsloggedIn()) { // 检查是否为POST请求 if ($_SERVER['REQUEST_METHOD'] == 'POST') { $post_id = $_POST['post_id']; $user_id = $_SESSION['user_id']; // 假设用户已登录,用户ID存储在session中 // 检查用户是否已点赞该帖子 if ($this->postModel->hasUserLikedPost($post_id, $user_id)) { // 用户已点赞,取消点赞 if ($this->postModel->removeLike($post_id, $user_id)) { // 减少帖子表中的点赞数 $this->postModel->decrementLikeCount($post_id); echo json_encode(['status' => 'unliked', 'likes_count' => $this->postModel->getLikesCount($post_id)]); } } else { // 用户未点赞,添加点赞 if ($this->postModel->addLike($post_id, $user_id)) { // 增加帖子表中的点赞数 $this->postModel->incrementLikeCount($post_id); echo json_encode(['status' => 'liked', 'likes_count' => $this->postModel->getLikesCount($post_id)]); } } } else { echo json_encode(['status' => 'error', 'message' => '无效请求']); } } else { echo json_encode(['status' => 'error', 'message' => '用户未登录']); } }
模型(Model)代码
public function hasUserLikedPost($user_id, $post_id) { echo $this->db->query('SELECT * FROM post_likes WHERE user_id = :user_id AND post_id = :post_id'); $this->db->bind(':user_id', $user_id); $this->db->bind(':post_id', $post_id); $row = $this->db->single(); return $row ? true : false; } public function addLike($post_id, $user_id) { $this->db->query('INSERT INTO post_likes (post_id, user_id) VALUES (:post_id, :user_id)'); $this->db->bind(':post_id', $post_id); $this->db->bind(':user_id', $user_id); return $this->db->execute(); } public function removeLike($post_id, $user_id) { $this->db->query('DELETE FROM post_likes WHERE post_id = :post_id AND user_id = :user_id'); $this->db->bind(':post_id', $post_id); $this->db->bind(':user_id', $user_id); return $this->db->execute(); } public function incrementLikeCount($post_id) { $this->db->query('UPDATE posts SET likes_count = likes_count + 1 WHERE id = :post_id'); $this->db->bind(':post_id', $post_id); return $this->db->execute(); } public function decrementLikeCount($post_id) { $this->db->query('UPDATE posts SET likes_count = likes_count - 1 WHERE id = :post_id'); $this->db->bind(':post_id', $post_id); return $this->db->execute(); } public function getLikesCount($post_id) { $this->db->query('SELECT likes_count FROM posts WHERE id = :post_id'); $this->db->bind(':post_id', $post_id); return $this->db->single()->likes_count; }
视图(View)代码
<button class="btn btn-success" id="upvote" data-post-id='<?php echo $data['post']->id; ?>'> <i class="fa fa-thumbs-up"></i> Upvote <span id="display"><?php echo $data['post']->likes_count; ?></span> </button>
AJAX代码
document.addEventListener("DOMContentLoaded", function () { let btn = document.getElementById("upvote"); if (!btn) { console.error('未找到ID为"upvote"的元素'); return; } let disp = document.getElementById("display"); let post_id = btn.getAttribute("data-post-id"); btn.onclick = function () { // 创建AJAX请求将post_id发送到PHP let xhr = new XMLHttpRequest(); xhr.open("POST", "<?php echo URLROOT;?>/posts/likepost", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { // 处理PHP返回的响应 let response = JSON.parse(xhr.responseText); if (response.status === 'liked') { disp.innerHTML = response.likes_count; // 更新点赞数 } else if (response.status === 'unliked') { disp.innerHTML = response.likes_count; // 更新点赞数 } } }; // 发送包含post_id的请求 xhr.send("post_id=" + post_id); } });
报错指向的show()方法(原正常运行)
public function show($id) { $post = $this->postModel->getPostById($id); $user = $this->userModel->getUserById($post->userid); $data = [ 'post' => $post, 'user' => $user, ]; $this->view('posts/show',$data); }
内容的提问来源于stack exchange,提问作者santosh bawari
相关产品推荐
相关产品推荐

