PHP投票系统

时间:2015-01-04 20:21:26

标签: javascript php jquery ajax

我有一个简单的PHP投票系统。我正在使用Ajax与会话来防止一个用户多次投票。我想在有人投票后给出一条“谢谢”的消息,如果某人已投票,我想给他们另一条消息。我的“谢谢”消息正在显示,但我的“已投票”消息却没有。为什么不呢?

scriptvote.php:

$(document).ready(function () {
    // ajax setup
    $.ajaxSetup({
        url: 'ajaxvote.php',
        type: 'POST',
        cache: 'false'
    });

    // any voting button (up/down) clicked event
    $('.vote').click(function () {
        var self = $(this); // cache $this
        var action = self.data('action'); // grab action data up/down
        var parent = self.parent().parent(); // grab grand parent .item
        var postid = parent.data('postid'); // grab post id from data-postid
        var score = parent.data('score'); // grab score form data-score

        // only works where is no disabled class
        if (!parent.hasClass('.disabled')) {
            // vote up action
            if (action == 'up') {
                // increase vote score and color to orange
                parent.find('.vote-score').html(++score).css({ color: 'orange' });

                // change vote up button color to orange
                alert('Thank You');
                self.css({ color: 'orange' });

                // send ajax request with post id & action
                $.ajax({
                    data: {
                        postid: postid,
                        action: 'up'
                    }
                });
            } else {
                alert(':(');
            }

            // voting down action

            // add disabled class with .item
            parent.addClass('.disabled');
        };
    });
});

ajaxvote.php:

<?php
include('config.php');
# start new session
session_start();

if ($_SERVER['HTTP_X_REQUESTED_WITH']) {
    if (isset($_POST['postid']) AND isset($_POST['action'])) {
        $postId = (int) mysql_real_escape_string($_POST['postid']);
        # check if already voted, if found voted then return
        if (isset($_SESSION['vote'][$postId])) return ' Already Voted';

        # connect mysql db
        dbConnect();

        # query into db table to know current voting score
        $query = mysql_query("
            SELECT vote
            from voting
            WHERE id = '{$postId}'
            LIMIT 1" );

        # increase or dicrease voting score
        if ($data = mysql_fetch_array($query)) {
            if ($_POST['action'] === 'up') {
                $vote = ++$data['vote'];
            } else {
                $vote = --$data['vote'];
            }
            # update new voting score
            mysql_query("
                UPDATE voting
                SET vote = '{$vote}'
                WHERE id = '{$postId}' ");

            # set session with post id as true
            $_SESSION['vote'][$postId] = true;
            # close db connection
            dbConnect(false);
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

当您想要将数据从PHP实际返回到ajax调用时,需要echo而不是return,然后在echo之后添加return以终止php脚本。

if (isset($_SESSION['vote'][$postId])) {
     echo ' Already Voted'; 
     return;
 }