这是我完整的投票脚本,效果很好。但我想只有用户可以在这里投票。那么我将在下面添加代码或其他任何方式来执行此操作?
include_once("session.php");
if($session->logged_in){
***vote process work***
} else { echo' login for vote here';}
这是我的HTML代码
<div class="voting_kochi" id="1013">
<div class="voting_btn">
<div class="up_button"> </div>
<span class="up_votes">0</span></div>
<div class="voting_btn">
<div class="down_button"> </div>
<span class="down_votes">0</span></div>
</div>
的javascript:
$(document).ready(function() {
//####### on page load, retrive votes for each content
$.each( $('.voting_kochi'), function(){
//retrive unique id from this voting_kochi element
var unique_id = $(this).attr("id");
//prepare post content
post_data = {'unique_id':unique_id, 'vote':'fetch'};
//send our data to "vote_process.php" using jQuery $.post()
$.post('../vote_process.php', post_data, function(response) {
//retrive votes from server, replace each vote count text
$('#'+unique_id+' .up_votes').text(response.vote_up);
$('#'+unique_id+' .down_votes').text(response.vote_down);
},'json');
});
//####### on button click, get user vote and send it to vote_process.php using jQuery $.post().
$(".voting_kochi .voting_btn").click(function (e) {
//get class name (down_button / up_button) of clicked element
var clicked_button = $(this).children().attr('class');
//get unique ID from voted parent element
var unique_id = $(this).parent().attr("id");
if(clicked_button==='down_button') //user disliked the content
{
//prepare post content
post_data = {'unique_id':unique_id, 'vote':'down'};
//send our data to "vote_process.php" using jQuery $.post()
$.post('../vote_process.php', post_data, function(data) {
//replace vote down count text with new values
$('#'+unique_id+' .down_votes').text(data);
}).fail(function(err) {
//alert user about the HTTP server error
alert(err.statusText);
});
}
else if(clicked_button==='up_button') //user liked the content
{
//prepare post content
post_data = {'unique_id':unique_id, 'vote':'up'};
//send our data to "vote_process.php" using jQuery $.post()
$.post('../vote_process.php', post_data, function(data) {
//replace vote up count text with new values
$('#'+unique_id+' .up_votes').text(data);
}).fail(function(err) {
//alert user about the HTTP server error
alert(err.statusText);
});
}
});
//end
});
vote_process.php
$ vote_up,'vote_down'=&gt; $ vote_down); echo json_encode($ send_response); //显示json编码值 打破; } } ?&GT;