나는 간단한 PHP 투표 시스템을 가지고 있습니다. 한 명의 사용자가 여러 번 투표하지 못하도록 세션에서 Ajax를 사용하고 있습니다. 누군가 투표 한 후 "감사합니다"라는 메시지를 전하고 싶습니다. 누군가 이미 투표를했다면 다른 메시지를주고 싶습니다. 내 "고맙습니다"메시지가 표시되지만 내 "이미 투표 됨"메시지는 표시되지 않습니다. 왜 안돼?PHP 투표 시스템
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에서 데이터를 반환 할 때, 다음 에코 후 수익을 추가하지 return
echo
필요
<?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);
}
}
}
?>
봅니다 경우 (parent.hasClass ('. 장애인')) {}'대신'다른 {}'문 – MAZux
그것은'parent.addClass를 비활성화하여 – user2349171