如何讓我的jQuery喜歡功能在專案中的每個貼文上都能正常運作
P粉111927962
P粉111927962 2023-09-16 11:56:50
0
1
1102

大家好。我正在設計一個類似社群媒體平台的平台,它有貼文、按讚和評論系統。我使用 jquery 來喜歡,每當我想發短信並且想喜歡一個帖子時,只有第一個帖子可以工作,但其他帖子的喜歡系統無法工作。我該如何解決它。 這是包含帖子和 jquery 程式碼的 index.php。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>post</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="font/css/all.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
            $(document).ready(function(){

                $("#likings").click(function(){
                    var name = $("input").val();
                    $.post("like.php", {
                        sugess: name
                    }, function(data, status){
                        $("#like").html(data);
                    });
                });
            });
    </script>
</head>
<?php
include('conn.php');
?>
<body>
    <?php
    $sql = "SELECT * FROM post";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($result)){
        $id = $row['id'];
        $name = $row['name'];
        $image = $row['image'];
        $action = $row['action'];
        $date = $row['date'];
        $time = $row['time'];
        $sqlcount = "SELECT * FROM likes WHERE postid=$id";
        $resultcount = mysqli_query($conn, $sqlcount);
        $count = mysqli_num_rows($resultcount);
        echo '
        <center>
            <div class="post">
                <div class="up">
                    <div>
                        <img class="img" src="img/'.$image.'">
                    </div>
                    <div class="uptext"><span class="name">'.$name.' </span><span class="des"> '.$action.'
                        <br>'.$date.' at '.$time.'</span></div>
                    <div>
                        ...
                    </div>
                </div>
                <img src="img/'.$image.'">
                <div class="liking">
                    <div class="like">
                        <i class="fa fa-thumbs-up" aria-hidden="true" style="text-align: left; color: navy;"></i><span id="like">'.$count.'</span>
                    </div>
                    <div>
                        <p>23 comments</p>
                    </div>
                </div>
                <div class="likenow">
                    <div>
                        <input name="id" value="'.$id.'" hidden>
                        <i class="fa fa-thumbs-up" id="likings" aria-hidden="true" style="text-align: left; color: navy;"></i>like
                    </div>
                    <div>
                        <i class="fa-solid fa-message"></i> comment
                    </div>
                </div>
                <p style="text-align: left; margin-left: 20px; font-size: 10pt;">view more comments</p>
                <div class="comments">
                    <p>itz celeb <br> very nice</p>
                    <p>titi kosi <br> so cute</p>
                </div>
                <div class="entercomment">
                    <div>
                        <img class="img" src="img/'.$image.'">
                    </div>
                    <div>
                        <input placeholder="Write a comment..."><i class="fa-solid fa-message" style="margin-left: 20px;"></i>
                    </div>
                </div>
            </div>
        </center>';
    }
    ?>
</body>
</html>

這是喜歡的 php 程式碼。

<?php
include("conn.php");
$name = $_POST['sugess'];
$sql = "INSERT INTO likes(postid, likes) VALUES('$name', '1')";
$result = mysqli_query($conn, $sql);
$sqlcount = "SELECT * FROM likes WHERE postid=$name";
$resultcount = mysqli_query($conn, $sqlcount);
$count = mysqli_num_rows($resultcount);
echo $count;
?>

那我該如何解決呢?

類似的內容應該適用於每個貼文。我使用了 while 循環,但我仍然遇到相同的問題。

P粉111927962
P粉111927962

全部回覆(1)
P粉959676410

你的問題出在選擇器上,它是一個ID選擇器,使用ID選擇器你只能選擇一個元素,嘗試將其更改為類別選擇器

<i class="fa fa-thumbs-up" id="likings" aria-hidden="true" style="text-align: left; color: navy;"></i>

<i class="fa fa-thumbs-up likings"  aria-hidden="true" style="text-align: left; color: navy;"></i>

應該可以

<script>
        $(document).ready(function(){

            $(".likings").click(function(){
                var name = $("input").val();
                $.post("like.php", {
                    sugess: name
                }, function(data, status){
                    $("#like").html(data);
                });
            });
        });
</script>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板