以下範例以 PHP 的 XAJAX 框架實作一個投票元件:選項在頁面載入時讀出,投票後直接在原位置顯示最新統計,不跳頁、不彈窗。
<html>
<head>
<title>投票</title>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
.poll { font-size: 10pt; display: block }
.pollresult { font-size: 12pt; display: none }
</style>
<?php
include_once("server1.server.php"); // XAJax 伺服器
$xajax->printJavascript();
?>
</head>
<body>
<script language="javascript">
function back() {
document.getElementById('poll').style.display = 'block';
document.getElementById('pollresult').style.display = 'none';
document.getElementById('pollresult').innerHTML = '';
}
</script>
<div id="pollresult" class="pollresult"></div>
<?php
global $db;
$poll = $db->getRow("select * from TBL_POLL order by poll_id desc limit 1");
$poll_id = $poll["poll_id"];
$pollitems = $db->getAll("select * from TBL_POLLITEM where poll_id=$poll_id");
?>
<div id="poll" class="poll">
<form id="pollForm" action="javascript:void(null);" onsubmit="onSubmit();">
<?php echo $poll["title"]; ?><br>
<?php for ($i = 0, $count = count($pollitems); $i < $count; $i++) { ?>
<input type="radio" style="background-color: #CCCCCC;"
name="pollitem"
value="<?php echo $pollitems[$i]['pollitem_id'] ?>">
<?php echo $pollitems[$i]['content'] ?><br>
<?php } ?>
<input type="hidden" name="poll_id" value="<?php echo $poll_id; ?>">
<input type="submit" value="投票">
</form>
<script language="javascript">
function onSubmit() {
xajax_poll(xajax.getFormValues("pollForm"));
document.getElementById('poll').style.display = 'none';
document.getElementById('pollresult').style.display = 'block';
}
</script>
</div>
</body>
</html>
幾個關鍵設計:action="javascript:void(null);" 搭配 onsubmit 攔截表單,讓表單永遠不會真的送出;xajax.getFormValues() 一次把整份表單(含隱藏的 poll_id)打包成陣列傳到後端;兩個 div 以 display 屬性互相切換,達到「原地換頁」的效果。
function poll($formData) {
global $db;
$tmp = "";
$objResponse = new xajaxResponse();
$poll_id = $formData['poll_id'];
$pollitem_id = $formData['pollitem'];
if ($pollitem_id > 0 && $poll_id > 0) {
$db->query("update " . TBL_POLLITEM . " set count = count + 1
where pollitem_id = $pollitem_id");
}
$poll = $db->getRow("select * from TBL_POLL where poll_id = $poll_id");
$pollitems = $db->getAll("select * from TBL_POLLITEM where poll_id = $poll_id");
$tmp .= "<div align=center>" . $poll["title"] . "</div><br>";
for ($i = 0, $count = count($pollitems); $i < $count; $i++) {
$tmp .= "<div align=left>" . $pollitems[$i]['content']
. ": " . $pollitems[$i]['count'] . "</div>";
}
$tmp .= "<div align=center>
<input type=\"button\" value=\"返回\" onclick=\"back();\">
</div>";
$objResponse->addAssign("pollresult", "innerHTML", $tmp);
return $objResponse->getXML();
}
回應不是自己拼 XML,而是用 addAssign() 指定「把某元素的 innerHTML 換成什麼」,由 XAJAX 產生並送出 XML 指令。返回前先重查一次統計,因此畫面上的票數一定是最新值。
CREATE TABLE TBL_POLL ( poll_id int(11) unsigned NOT NULL default '0', title varchar(100) NOT NULL default '', created_date bigint(20) unsigned NOT NULL default '0', user_id int(11) unsigned NOT NULL default '0', PRIMARY KEY (poll_id) ); CREATE TABLE TBL_POLLITEM ( pollitem_id int(11) unsigned NOT NULL default '0', poll_id int(11) unsigned NOT NULL default '0', content varchar(100) NOT NULL default '', count int(11) unsigned NOT NULL default '0', PRIMARY KEY (pollitem_id) );
範例把 $poll_id 直接插入 SQL,這是可被竄改的注入點,正式環境請改用 prepared statement 並以 intval() 強制型別。其次,投票沒有任何去重複機制,同一個使用者可以反覆累積票數;一般會以 session、Cookie 或登入帳號搭配唯一索引來限制。再者,count 是 SQL 保留字,欄位建議改名(如 vote_count)並在語句中加上反引號或表別名前綴,避免不同版本資料庫解析出錯。最後,輸出到頁面的標題與選項文字應先經 htmlspecialchars() 轉義,否則使用者輸入的內容會被當成 HTML 執行。