首页 | 论坛 | 新闻资讯 | 设计欣赏 | 设计教程 | 网络编程 | 字体下载 | 平面教程 | 特色图标 | 设计素材 | 代码素材 | 网页模板 | 建站服务
卡通 | 环境 | 平面设计 | 网页设计 | 广告艺术 | 时尚摄影 | 形像设计 | 插画设计 | 三维设计 | 工业设计 | 时尚摄影 | 时尚时装 | 界面设计
·企业建站只需980元起
·制作大型专业网站,亦是如此简单
·南宁网站建设  ·南宁网站建设
·南宁网站建设  ·南宁网站建设
 当前位置:主页>网络编程>Ajax教程>列表
实现一个基于 Ajax 的调查程序
2008-07-21  来源:网络收集   作者:未知  点击:   评论:0 条

<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;

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

[收藏]  [推荐]  [评论]  [打印]  [关闭]
上一篇:ASP.NET+AJAX 解决网页打开等待问题
下一篇:没有了
 最新图文
 最新评论
查看所有评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: (新注册) 密码:
匿名?
免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有异议,请联络本站! 转载要求:文章作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站地址,且不准打上各自站点的水印。
 热门关注
 相关文章
 Google提供的广告