DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 實現一個基於Ajax的調查程序
實現一個基於Ajax的調查程序
編輯:AJAX詳解     
 

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

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved