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

该博客展示了一个基于Ajax、PHP、JavaScript、CSS和HTML的投票系统。前端通过HTML和CSS构建页面,JavaScript处理交互逻辑,利用Ajax实现无刷新投票。后端使用PHP处理投票数据,更新数据库。还给出了数据库表的创建语句,投票后原地显示最新信息,无需弹出窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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"); #servidor para 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="enter">
</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>

服务器端

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();
}

数据库的表如下

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;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值