我正在寻找如何在php中实现StackOverflow/reddit投票系统的示例.
基本上我想要向上和向下箭头框.那里有什么好的例子吗?
有很多脚本,但你自己并不难.
我之前使用过jQuery(处理AJAX)和一个小的PHP脚本.例如,一些伪代码:
// 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'));
}
Run Code Online (Sandbox Code Playgroud)
然后一些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
}
Run Code Online (Sandbox Code Playgroud)