實現一個基於Ajax的調查程式

來源:互聯網
上載者:User

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

這個例子中,調查的選項只在頁面裝載時讀出,投票後在原地顯示最新的投票資訊。不需要快顯視窗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.