weixin_33690367 2015-01-04 20:21 采纳率: 0%
浏览 41

PHP投票系统

I have a simple PHP voting system. I’m using Ajax with sessions to prevent multiple votes from one user. I want to give a “Thank you” message after someone votes, and if someone has already voted, I want to give them another message. My “Thank you” message is showing, but my “Already voted” message is not. Why not?

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条回答 默认 最新

  • weixin_33692284 2015-01-04 20:40
    关注

    You need to echo not return when you want to actually return data from PHP to ajax call, then add return after echo to terminate the php script.

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加