Stack Overflow / reddit voting system in php

前端 未结 1 468
遇见更好的自我
遇见更好的自我 2020-12-05 22:02

I\'m looking for examples of how to implement a StackOverflow / reddit voting system in php.

Basically I want the Up and Down arrow box. Are there any good examples

相关标签:
1条回答
  • 2020-12-05 22:43

    There are lots of scripts out there but it's not too hard to do yourself.

    I've used jQuery (to handle AJAX) and a small PHP script before. For example, some pseudo-code:

    // Some checking for recent votes from this user is appropriate here
    if (isset($_POST['voteType'], $_POST['postId']) && $user->loggedIn) {
        // insert vote into database if not already inserted
        echo json_encode(array('error' => false));
    } else {
        // bad request/hack attempt
        echo json_encode(array('error' => true, 'message' => 'Bad parameters sent'));
    }
    

    and then some jQuery:

    $('#upVote').click(function() {
        $.post('vote.php', {voteType: 'up', postId: 42}, 'updateIcon(data, textStatus)', 'json');
    });
    
    function updateIcon(data, textStatus) {
        // If error = false highlight the upvote icon
        // else show the error message returned
    }
    

    jQuery.post

    0 讨论(0)
提交回复
热议问题