返回POST所在地



如果有人知道怎么做。。。我创建了博客,那里有很多帖子。每个帖子都有"赞"、"不赞"one_answers"评论"(就像在许多帖子网站上一样(。当我点击其中一个帖子的'like'(在元素表单内部的html部分(时,我通过表单元素提交post。然后在同一文件的php中,我增加了数据库中"like"的值。这个值是通过数据库中更新的适当帖子中显示的echo显示的。问题是,通过做这个更新,我不会回到增加"赞"的地方。如何返回我提交POST的位置?例如,我在Instagram上看到,当我点击"like"时,这个地方没有移动,停留在点击的同一个地方。非常感谢你让我知道。。。

<?php
$conn = mysqli_connect("localhost", "root", "", "amoscaguias_db");
if(!$conn) {
echo "<h3>No connection to database</h3>";
}

if(isset($_POST['increase-btn'])) {
$post_id = $_POST['post_id'];
$value = 0;
$stmt= $conn->prepare("SELECT very_like FROM blog_data WHERE id=? LIMIT 1");
$stmt->bind_param('i', $post_id);
$stmt->execute();

$result = $stmt->get_result();
$row = $result->fetch_assoc();
$result_1 = $row['very_like'];
$result_1 = $result_1 + 1;
$stmt1 = $conn->prepare("UPDATE blog_data SET very_like=$result_1 WHERE id=$post_id");
$stmt1->execute();

header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" 
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>blog_php_mysql</title>
</head>
<body>
<div class="row">
<?php foreach($query as $q) { ?>
<div class="col-12 post">
<div class="card post-card mt-5">
<img class="card-img-top img-thumbnail post-card-image" src="imgs/<?php if(!empty($q['image'])) { echo $q['image']; } ?>" alt="<?php if(!empty($q['image'])) { echo $q['image']; } ?>">
<div class="card-body post-card-body">
<h5 class="card-title"><?php echo $q['title']; ?></h5>
<hr class="mx-auto">
<p class="card-text"><?php echo $q['content']; ?></p>
<hr class="mx-auto">
<div class="row ">
<div class="col-3 post-footer">
<form action="index.php" method="POST">
<button type="submit" id="increase" class="increase_btn" name="increase-btn"><i class="bi bi-hand-thumbs-up"></i></button>
<input type="hidden" name="post_id" value="<?php echo $q['id'];  ?>" />
</form>  
<span><?php echo $q['very_like'] ?></span> 
<form action="index.php" method="POST">
<button type="submit" id="decrease" class="decrease_btn" name="decrease-btn"><i class="bi bi-hand-thumbs-down"></i></button>
<input type="hidden" name="post_id" value="<?php echo $q['id'];  ?>" />
</form>  
<span><?php echo $q['dont_like'] ?></span> 
<a href="#" class="post-comments">Comments</a>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">
</script> 
<script src="js/myscript.js"></script>   
</body>
</html>

有几种方法可以做到这一点:

  1. 常见而漂亮的方法:通过javascript向后端的单独操作(端点(发出XHR请求,在那里您将从POST数组中提取数据,更新数据库中的值,并以JSON格式返回响应,您可以轻松解析并设置新值。此外,有时还有一种方法是在后端发送关于成功更新的确认之前通过javascript增加点赞——这是因为点赞不是那么重要的数据
  2. 不太常见也有点难看的方式:你可以在每个帖子上添加一个锚链接。当你点击"点赞"按钮时,你应该发送额外的数据,其中包括在后端获取该锚链接的方式。在后台成功更新值后,你应该重定向到上一个页面,并将锚点指向喜欢的帖子,这样你就会"返回相同的地方";,因为浏览器将自动将页面滚动到具有该锚的块