gpt4 book ai didi

php - 在页面刷新时保持按钮状态?

转载 作者:行者123 更新时间:2023-11-29 08:00:33 26 4
gpt4 key购买 nike

我的index.php 页面上有一个“喜欢”按钮,单击时该按钮会递增并更改为“喜欢”。这太棒了。问题是,当我重新加载页面时,“喜欢”按钮会返回到原来的“喜欢”状态。

我的问题是,即使在页面刷新时如何保持按钮为“喜欢”?

我假设在我的 likes 表中,单击按钮时我必须将 liked_unliked 设置为“1”。但是,更改“喜欢”按钮状态的 JQuery 如何知道有一个“1”,这意味着用户已经在该图像上单击了“喜欢”,即使在页面刷新时,这也会保持“喜欢”状态?

这是我的index.php:

<!doctype html>
<?php
session_start();
?>
<html>
<head>

<?php
include('header.php');
?>

<script>
//send info to like_button.php then to DB
$(document).ready(function(){
$(".button").click(function(){
$.ajax({url:"liked_button.php",success:function(result){
//do nothing
}});
});
});
//change like button when clicked
$(document).ready(function(){
$(".button").click(function(){
var t=$(this);
t.replaceWith("<button type='button' class='button_pressed'>Liked</button>");
});
});
//increment like counter when button is clicked
function increase(t) {
t=$(t).parent().next();
t.val(parseInt(t.val())+1);
}
</script>

</head>
<link type="text/css" rel="stylesheet" href="index.css">
<body>

<?php
//dispay the images that the users upload
$conn = mysqli_connect("localhost","root","") or die ("No SQLI");
mysqli_select_db($conn, "sample") or die ("No DB");

$sqli = "SELECT * FROM `photos` ORDER BY `id` DESC";

$result = mysqli_query($conn, $sqli) or die ("No query");

while($row = mysqli_fetch_assoc($result))

{
$username = $row['username'];
$title = $row['title'];
$description = $row['description'];
$image_name = $row['image_name'];
$image_id = $row['image_id'];
$random_directory = $row['random_direc'];
$date = date('Y-m-d');

$image_info = "http://localhost/splindr_2.0/photos/$random_directory/$image_name";
//echo all content in content wrapper
echo "<div id=contentWrapper'>
<div class='photo'>
<div class='actual_image'>
<img src='$image_info'>
</div>
<div class='like_system'><button onClick='increase(this)' type='button' class='button' id='button' name='button'>Like</button></div>
<input type='text' name='total_likes' id='total_likes' value='0'>
<div class='twitter'><button type='button' class='twitter_button' name='twitter_button' id='twitter_button'>
<a style='color: #2e4987;text-decoration:none' href='http://www.twitter.com/share?url=$image_info&hashtags=Splindr, CheckThisOut'>Share On Twitter</a></button>
</div>
<div class='trash_bin'>
<button type='button' class='delete_button' name='delete_button' id='delete_button'>
<a style='text-decoration:none;color: #2e4987' href='delete_post.php'>Delete</a>
</button>
</div>
<div class='info_wrapper'>
<div class='info_header'>Title: $title &nbsp By: $username &nbsp Date: $date</div>
<div class='description'>$description</div>
</div>
</div>
</div>
</div>";//end contentWrapper

}

?>

</body>
</html>

我的喜欢表:

CREATE TABLE if not exists `likes` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`image_id` varchar(300) NOT NULL,
`liked_by` varchar(50) NOT NULL,
`liked_unliked` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

最佳答案

您正在跟踪谁按下了我假设在 Liked_by 字段中的“赞”按钮。您可以将用户的喜好添加到 session 中。这样,如果您刷新页面,您就会在 Like session 中获得 user_id,并相应地调整按钮的 CSS。

    <?php
$_SESSION['likes'] = array(
'img1' => 'user2, user3',
'img2' => 'user2',
'img3' => 'user2','user5',
);

$likes = $_SESSION['likes'] ;

foreach($likes as $key => $like){
$likeArray = explode(',', $like); //convert string of user ids to array

if(in_array($userId, $likeArray)){
//user has like image with id $key
}else{
//user has not
}

}
?>

关于php - 在页面刷新时保持按钮状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23939421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com