实现一个基于Ajax的调查程序

 1<html>
 2<head>
 3<title>投票</title>
 4<meta content="zh-cn" http-equiv="Content-Language"/>
 5<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 6<style type="text/css">   
 7<!--   
 8.poll {font-size: 10pt;display:block}   
 9.pollresult {font-size: 12pt;display:none}   
10\-->   
11</style>   

include_once("server1.server.php"); #servidor para XAJAX
$xajax-&gt;printJavascript();

 1
 2</head>
 3<body>
 4<script language="javascript">   
 5function back() {   
 6document.getElementById('poll').style.display = 'block';   
 7document.getElementById('pollresult').style.display = 'none';   
 8document.getElementById('pollresult').innerHTML = '';   
 9}   
10</script>
11<div class="pollresult" id="pollresult">
12</div>
13
14  

global $db;

$poll = $db-&gt;getRow("select * from TBL_POLL order by poll_id desc limit 1");
$poll_id = $poll["poll_id"];
$pollitems = $db-&gt;getAll("select * from TBL_POLLITEM where poll_id=$poll_id");

1<div class="poll" id="poll">
2<form action="javascript:void(null);" id="pollForm" onsubmit="onSubmit();">   

echo $poll["title"];

for ($i = 0, $count = count($pollitems); $i &lt; $count; $i++) {

1<input name="pollitem" style="background-color : #CCCCCC;" type="radio" value="```
2 echo $pollitems[$i]['pollitem_id'] 
3```"/>```
4 echo $pollitems[$i]['content'] 
5```<br/>   

}

 1<input name="poll_id" type="hidden" value="```
 2 echo $poll_id; 
 3```"/>
 4<input type="submit" value="enter"/>
 5</form>
 6<script language="javascript">   
 7function onSubmit() {   
 8xajax_poll(xajax.getFormValues("pollForm"));   
 9document.getElementById('poll').style.display = 'none';   
10document.getElementById('pollresult').style.display = 'block';   
11}   
12</script>
13</div>
14</body>
15</html>

服务器端

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 .="

1<div align="center">".$poll["title"]."</div>
1<br/>

";
for ($i = 0, $count = count($pollitems); $i < $count; $i++) {
$tmp .="

1<div align="left">".$pollitems[$i]['content'].": ".$pollitems[$i]['count']."</div>

";
}
$tmp .="

1<div align="center">"."<input onclick='\"back();\"' type='\"button\"' value='\"返回\"'/>"."</div>

";

$objResponse->addAssign("pollresult","innerHTML",$tmp);
return $objResponse->getXML();
}

数据库的表如下

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)
) TYPE=MyISAM;

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)
) TYPE=MyISAM;

这个例子中,调查的选项只在页面装载时读出,投票后在原地显示最新的投票信息。不需要弹出窗口

Published At
Categories with Web编程
Tagged with
comments powered by Disqus