PHP Ajax Like System

时间:2015-02-10 00:34:15

标签: javascript php jquery ajax

我正在为我的网站评论系统制作喜欢/不喜欢的系统。

我正在使用本教程http://www.webcodo.net/like-dislike-system-with-jquery-ajax-and-php-youtube-like-design/

除了我在每个页面上都有多个喜欢/不喜欢的按钮,因为有多个评论,人们有能力喜欢/不喜欢。

我是ajax btw的新手。

这是javascript文件。

$('.like-btn').click(function(){
    $('.dislike-btn').removeClass('dislike-h');    
    $(this).addClass('like-h');
    $.ajax({
        type:"POST",
        url:"../outfits/outfit.php",
        data:'act=like&function=true&pageID=' + pageID + '&user=' + email,
        success: function(){
        }
    });
});

$('.dislike-btn').click(function(){
    $('.like-btn').removeClass('like-h');
    $(this).addClass('dislike-h');
    $.ajax({
        type:"POST",
        url:"../outfits/outfit.php",
        data:'act=dislike&function=true&pageID=' + pageID + '&user=' + email,
        success: function(){
        }
    });
});

这是"评论行"

<div class="row">
                    <h5>Comments</h5>
                    <div class="row">
                        <div class="small-12 columns">
                            <textarea rows="4" class="post-comment" placeholder="You can comment or rate without doing the other" name="comment" maxlength="140"></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="small-10 large-10 columns">
                            <div class="rating">
<input type="radio" id="star5" name="rating" value="5" /><label for="star5">5 stars</label>
<input type="radio" id="star4" name="rating" value="4" /><label for="star4">4 stars</label>
<input type="radio" id="star3" name="rating" value="3" /><label for="star3">3 stars</label>
<input type="radio" id="star2" name="rating" value="2" /><label for="star2">2 stars</label>
<input type="radio" id="star1" name="rating" value="1" /><label for="star1">1 star</label>
                            </div>
                        </div>
                        <div class="small-2 large-2 columns">
                            <input type="submit" name="postComment" class="small button radius login-button right" value="Post">
                        </div>
                    </div>
                    <div class="row">
                        <div class="small-12 columns">
                            <hr />
                        </div>
                    </div>
                    <?php
                        foreach ($commentRows as $commentRow) {
                                try {
                                $pageID = $commentRow['id']; // The ID of the page, the article or the video ...

                                    extract($_POST);

                                    $dislike_sql = 'SELECT * FROM  likes WHERE user = "'.$email.'" and comment_id = "'.$pageID.'" and rate = 2 ';
                                    $stmt = $dbhandle->prepare($dislike_sql);
                                    $stmt->execute();

                                    $dislike_count = $stmt->rowCount();


                                    $like_sql = 'SELECT * FROM  likes WHERE user = "'.$email.'" and comment_id = "'.$pageID.'" and rate = 1 ';

                                    $stmt = $dbhandle->prepare($like_sql);
                                    $stmt->execute();

                                    $like_count = $stmt->rowCount();


                                    if($act == 'like'): //if the user click on "like"
                                    if(($like_count == 0) && ($dislike_count == 0)){

                                        $rate = 1;
                                        $sql = 'INSERT INTO likes (comment_id, user, rate )VALUES(:pageID, :email, :rate)';

                                        //prepare data for insert
                                        $stmt = $dbhandle->prepare($sql);

                                        $stmt->bindValue(":pageID", $pageID);
                                        $stmt->bindValue(":email", $email);
                                        $stmt->bindValue(":rate", $rate);

                                        //Execute
                                        $stmt->execute();
                                    }
                                    if($dislike_count == 1){

                                        $rate = 1;

                                        $sql = 'UPDATE likes SET rate = ? WHERE comment_id = ? and user = ?';

                                        $stmt = $dbhandle->prepare($sql);
                                        $stmt->execute(array($rate, $pageID, $email));
                                    }
                                    endif;

                                    if($act == 'dislike'): //if the user click on "like"
                                    if(($like_count == 0) && ($dislike_count == 0)){

                                        $rate = 2;
                                        $sql = 'INSERT INTO likes (comment_id, user, rate )VALUES("'.$pageID.'", "'.$email.'", "2")';

                                        //prepare data for insert
                                        $stmt = $dbhandle->prepare($sql);

                                        $stmt->bindValue(":pageID", $pageID);
                                        $stmt->bindValue(":email", $email);
                                        $stmt->bindValue(":rate", $rate);

                                        //Execute
                                        $stmt->execute();
                                    }
                                    if($like_count == 1){

                                        $rate = 2;
                                        $sql = 'UPDATE likes SET rate = ? WHERE comment_id = ? and user = ?';

                                        $stmt = $dbhandle->prepare($sql);
                                        $stmt->execute(array($rate, $pageID, $email));
                                    }
                                    endif;
                                }
                                catch (PDOException $e) {
                                    echo $e->getMessage();   
                                }
                                print '<div class="row comment-row" id="gohere">
                                <div class="small-12 columns">
                                    <div class="small-12 columns">
                                        <div class="row">
                                            <div class="small-12 large-2 columns text-center">
                                                <div class="row">
                                                    <div class="small-12 columns">
                                                        <span>' . $commentRow['first-name'] . '</span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="small-12 columns">
                                                        <img src="https://a3-images.myspacecdn.com/images03/1/240e42b5d9ce48a78983961e7fcb3c39/300x300.jpg" class="profile-pic-comment" />
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="small-12 large-10 columns comments">
                                                <div class="small-12 columns comment-text">
                                                    <span>' . $commentRow['text'] . '</span>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="small-12 large-4 columns right">
                                                    <div class="small-6 columns">
                                                        <div class="like-btn' . $commentRow['id'] . ' ';
                                                        if($like_count == 1){
                                                            echo 'like-h';
                                                        }

                                                        print '">Like</div>
                                                    </div>
                                                    <div class="small-6 columns">
                                                        <div class="dislike-btn' . $commentRow['id'] . ' ';

                                                        if($dislike_count == 1){
                                                            echo 'dislike-h';
                                                        }
                                                        print '"></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>';
                            }
                    ?>
                </div>

现在我可以看到喜欢/不喜欢的计数,因为插入和更新查询不起作用,所以这些都是零。我手动输入数据以查看计数是否正常。

非常感谢任何帮助。如果我需要发布更多细节/代码,请告诉我。

1 个答案:

答案 0 :(得分:0)

使用rowCount()函数代码尝试count()。

$CountLikes = count($smpt);

我不认为这是问题所在。无论如何,既然你说你是AJAX的新手,你是否尝试过使用没有AJAX的类似系统。不是说,问题在于AJAX,但它会排除你的问题是服务器端还是AJAX问题。排除问题所在将对您有所帮助。

如果它在没有AJAX的情况下成功运行,那么我们可以假设AJAX没有正确地将数据发送到服务器。你可以在那里工作。