紅藍雙方觀點對抗的投票情境很常見——「實用」對「看不懂」、「贊成」對「反對」。本文以紅藍投票為例,透過前後台互動即時呈現雙方票數與占比,並用 IP 記錄攔住重複投票。

這是綜合型範例,需要具備 PHP、MySQL、jQuery 與 HTML/CSS 的基礎。資料表沿用「頂/踩」投票那篇的設計,可直接參考。
<div class="vote">
<div class="votetitle">您對提供的文章的看法?</div>
<div class="votetxt">非常實用<span>完全看不懂</span></div>
<div class="red" id="red">
<div class="redhand"></div>
<div class="redbar" id="red_bar"><span></span><p id="red_num"></p></div>
</div>
<div class="blue" id="blue">
<div class="bluehand"></div>
<div class="bluebar" id="blue_bar"><span></span><p id="blue_num"></p></div>
</div>
</div>
#red 與 #blue 分別代表兩方;.redhand/.bluehand 是手型投票鈕,.redbar/.bluebar 是比例條,#red_num/#blue_num 顯示票數。
.vote{width:288px; height:220px; margin:60px auto 20px auto; position:relative}
.votetitle{width:100%; height:62px; background:url(icon.png) no-repeat 0 30px; font-size:15px}
.red{position:absolute; left:0; top:90px; height:80px;}
.blue{position:absolute; right:0; top:90px; height:80px;}
.votetxt{line-height:24px}
.votetxt span{float:right}
.redhand{position:absolute; left:0; width:36px; height:36px; background:url(icon.png) no-repeat -1px -38px; cursor:pointer}
.bluehand{position:absolute; right:0; width:36px; height:36px; background:url(icon.png) no-repeat -41px -38px; cursor:pointer}
.grayhand{width:34px; height:34px; background:url(icon.png) no-repeat -83px -38px; cursor:pointer}
.redbar{position:absolute; left:42px; margin-top:8px;}
.bluebar{position:absolute; right:42px; margin-top:8px;}
.redbar span{display:block; height:6px; background:red; width:100%; border-radius:4px;}
.bluebar span{display:block; height:6px; background:#09f; width:100%; border-radius:4px; position:absolute; right:0}
.redbar p{line-height:20px; color:red;}
.bluebar p{line-height:20px; color:#09f; text-align:right; margin-top:6px}
點擊手型鈕時,用 $.getJSON() 向後端送請求,取回 JSON 後更新票數與比例條寬度。
function getdata(url, sid) {
$.getJSON(url, { id: sid }, function (data) {
if (data.success == 1) {
var w = 208; // 比例條總寬
// 紅方
$("#red_num").html(data.red);
$("#red").css("width", data.red_percent * 100 + "%");
$("#red_bar").css("width", w * data.red_percent - 10);
// 藍方
$("#blue_num").html(data.blue);
$("#blue").css("width", data.blue_percent * 100 + "%");
$("#blue_bar").css("width", w * data.blue_percent);
} else {
alert(data.msg);
}
});
}
$(function () {
getdata("vote.php", 1); // 初始資料
$(".redhand").click(function () { getdata("vote.php?action=red", 1); });
$(".bluehand").click(function () { getdata("vote.php?action=blue", 1); });
});
這裡的 sid 對應資料表的 id,範例固定為 1,實際專案要依主題動態帶入。
include_once("connect.php");
$action = $_GET['action'];
$id = intval($_GET['id']);
$ip = get_client_ip();
if ($action == 'red') { // 紅方投票
vote(1, $id, $ip);
} elseif ($action == 'blue') { // 藍方投票
vote(0, $id, $ip);
} else { // 預設回傳初始資料
echo jsons($id);
}
vote() 先查 votes_ip 表看這個 IP 投過沒;沒投過才更新票數並寫入 IP 記錄,避免重複灌票:
function vote($type, $id, $ip) {
$ip_sql = mysql_query("select ip from votes_ip where vid='$id' and ip='$ip'");
$count = mysql_num_rows($ip_sql);
if ($count == 0) {
if ($type == 1) {
$sql = "update votes set likes=likes+1 where id=" . $id;
} else {
$sql = "update votes set unlikes=unlikes+1 where id=" . $id;
}
mysql_query($sql);
mysql_query("insert into votes_ip (vid, ip) values ('$id','$ip')");
if (mysql_insert_id() > 0) {
echo jsons($id);
} else {
$arr['success'] = 0; $arr['msg'] = '操作失敗,請重試';
echo json_encode($arr);
}
} else {
$arr['success'] = 0; $arr['msg'] = '已經投票過了';
echo json_encode($arr);
}
}
jsons() 負責算比例並輸出 JSON:
function jsons($id) {
$query = mysql_query("select * from votes where id=" . $id);
$row = mysql_fetch_array($query);
$red = $row['likes'];
$blue = $row['unlikes'];
$arr['success'] = 1;
$arr['red'] = $red;
$arr['blue'] = $blue;
$red_percent = round($red / ($red + $blue), 3);
$arr['red_percent'] = $red_percent;
$arr['blue_percent'] = 1 - $red_percent;
return json_encode($arr);
}
CREATE TABLE IF NOT EXISTS `votes` ( `id` int(10) NOT NULL AUTO_INCREMENT, `likes` int(10) NOT NULL DEFAULT '0', `unlikes` int(10) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES (1, 30, 10); CREATE TABLE IF NOT EXISTS `votes_ip` ( `id` int(10) NOT NULL, `vid` int(10) NOT NULL, `ip` varchar(40) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
votes 存雙方總票數,votes_ip 存投票紀錄。範例若跑不起來,第一步永遠是檢查資料庫連線設定。
另外提醒:範例中的 SQL 是直接拼接字串,正式環境請改用 PDO 或 mysqli 的參數綁定,避免 SQL Injection。