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

来源:互联网 发布:c语言开发 编辑:程序博客网 时间:2024/04/30 18:54



投票





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



    function back() {
      document.getElementById('poll').style.display = 'block';
      document.getElementById('pollresult').style.display = 'none';
      document.getElementById('pollresult').innerHTML = '';
    }
 



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");
?>



  

  
 

 
 
 


    function onSubmit() {
      xajax_poll(xajax.getFormValues("pollForm"));
      document.getElementById('poll').style.display = 'none';
      document.getElementById('pollresult').style.display = 'block';
    }


服务器端

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

".$poll["title"]."

";
    for ($i = 0, $count = count($pollitems); $i < $count; $i++) {
      $tmp .="
".$pollitems[$i]['content'].": ".$pollitems[$i]['count']."
";
    }
  $tmp .="
".""."
";
 
  $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;

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



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=514436


原创粉丝点击