+ -
当前位置:首页 → 问答吧 → 简单投票页6.6KB,有需要的可以看下。

简单投票页6.6KB,有需要的可以看下。

时间:2010-09-14

来源:互联网

只写了单选,有需要的可以自己修改下。


'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" title="Click Here To EnLarge">

上源码:
复制代码
  1.  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3.  <HEAD>
  4.   <TITLE> 内部投票系统 </TITLE>
  5.  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.  </HEAD>
  7.  <style>
  8. html, body, div {
  9. background-color:#404040;
  10. color:#ffffff;
  11. font:14px/1.5em Tahoma,Verdana,Helvetica,微软雅黑,宋体,Arial,"Arial Unicode MS",MingLiu,PMingLiu,"MS Gothic",sans-serief;
  12. margin:0;
  13. padding:0;
  14. }
  15.  .all{
  16.  width:300px;
  17.  padding:10px;
  18.  margin-left:300px;
  19.  }
  20.  .all .head{
  21.   background-color:#ffffff;
  22.   color:#000000;
  23.   border:1px solid #fff;
  24.   font-size:22px;
  25.   text-align:center;
  26.  }
  27.  .all .body .item{
  28.  padding:3px 10px 2px 10px;
  29.  font-size:16px;
  30.  border:1px dashed #FC8916;
  31.  position:relative;
  32.  }
  33. .all .body .item label{
  34.  cursor:pointer;
  35. }
  36. .all .body .item div{
  37.  font-size:16px;
  38.  font-weight:bold;
  39.  color:#F9FFB5;
  40.  position:absolute;right:10px;top:0;
  41.  _margin:0 10px 0 0;
  42. }
  43.  .all .body div.item:hover{
  44.  background-color:#DDE388;
  45.  color:FC8916;
  46.  font-weight:bold;
  47.  }
  48.  .all .foot{
  49.  text-align:center;
  50.  margin-top:10px;
  51.  }
  52.  .all .foot input{
  53.  border:1px dashed #fff;
  54.  background-color:#7D7E7A;
  55.  cursor:pointer;
  56.  color:#F9FFB5;
  57.  font-size:16px;
  58.  font-weight:bold;
  59.  }
  60.  #result_head{
  61.  color:#B9B9FF;
  62.  font-size:18px;
  63.  font-weight:bold;
  64.  margin-left:380px;
  65.  }
  66.  #detail:link,a:visited,a:active{
  67.  color:#FC8916;
  68.  text-decoration:none;
  69.  font-size:14px;
  70.  }
  71.  #detail:hover{
  72.  color:#FFFFCC;
  73.  text-decoration:underline;
  74.  }
  75.  #result_content1{
  76.  display:none;
  77.  margin:10px 30px;
  78.  border:1px dashed #FC8916;
  79.  padding:5px;
  80.  }
  81.  #result_content1 div span{
  82.  margin-left:3px;
  83.  color:#F9FFB5;
  84.  }
  85.  #result_content2{
  86.  margin:10px 10px 10px 300px;
  87.  }
  88.  #result_content2 div{
  89.  width:300px;
  90.  background-color:#FFFFFF;
  91.  color:#000;
  92.  padding:3px 10px 2px;
  93.  }
  94.  #result_content2 div span{
  95.  color:#800000;
  96.  font-weight:bold;
  97.  margin:0 5px 0 3px;
  98.  }
  99.  </style>
  100.  <BODY>
  101.  <script type="text/javascript">
  102.  //显示/隐藏详细
  103.   function show_detail(){
  104.   var result = document.getElementById('result_content1');
  105.   var detail = document.getElementById('detail');
  106.   if(result.style.display != "block"){
  107.    result.style.display = "block";
  108.    detail.innerHTML= "(隐藏投票人)";
  109.   }else{
  110.    result.style.display = "none";
  111.    detail.innerHTML= "(查看投票人)";   
  112.   }
  113.   }
  114.  </script>
  115. <?php
  116. /*------------------------------------------------------
  117. *
  118. *    用户填写开始
  119. --------------------------------------------------------*/
  120.  $title = '最近想看的电影';   //填写标题
  121.  $single_choice = 1;  //是否单选 1是,0否
  122.  $content = array(  //填写投票选项
  123.   '盗梦空间',
  124.   '初恋红豆冰',
  125.   '敢死队',
  126.   '举起手来2:追击阿多丸',
  127.   '魔法师的学徒',
  128.   '唐山大地震',
  129.   '线人'
  130.  );  
  131. /*------------------------------------------------------
  132. *
  133. *    用户填写结束
  134. --------------------------------------------------------*/ 
  135.  $type = $single_choice?'radio':'checkbox';
  136.  $is_voted = 0;
  137.  //保存文件
  138.  $filename = 'result.txt';
  139.  //检查是否已投过
  140.  $user_ip = $_SERVER['REMOTE_ADDR'];
  141.  $user_name = gethostbyaddr($_SERVER['REMOTE_ADDR']);
  142.  $pop = safe_file_get_contents($filename);
  143.  if(!empty($pop)){
  144.   $pop = json_decode($pop);
  145.   $pop = object_array($pop);
  146.   $user_votes = $pop['votes'];
  147.   if(!empty($user_votes)){
  148.    foreach($user_votes as $p){
  149.     if($user_ip == $p['ip']){
  150.      $is_voted = 1;
  151.      break;
  152.     }
  153.    }
  154.   }
  155.  }
  156.  //投票
  157.  $user_vote = $_POST['myvote'];
  158.  $push = array();
  159.  if(!empty($user_vote) && !$is_voted && preg_match('/^[1-9]\d?$/',$user_vote)){
  160.   $txt = safe_file_get_contents($filename);
  161.   if(empty($txt)){
  162.    $push['votes'][] = array("ip"=>$user_ip,"name"=>$user_name,"vote"=>$user_vote);
  163.   }else{
  164.    $push = json_decode($txt);
  165.    $push = object_array($push);
  166.    $push['votes'][] = array("ip"=>$user_ip,"name"=>$user_name,"vote"=>$user_vote);
  167.   }
  168.   //写入文件
  169.   safe_file_put_contents($filename,json_encode($push));
  170.   $is_voted = 1;
  171.  }
  172. //对象转数组
  173. function object_array($array)
  174. {
  175.    if(is_object($array))
  176.    {
  177.     $array = (array)$array;
  178.    }
  179.    if(is_array($array))
  180.    {
  181.     foreach($array as $key=>$value)
  182.     {
  183.      $array[$key] = object_array($value);
  184.     }
  185.    }
  186.    return $array;
  187. }
  188. //写文件
  189. function safe_file_put_contents($filename, $content)
  190. {
  191.    $fp = fopen($filename, 'wb');
  192.    if ($fp) {
  193.     flock($fp, LOCK_EX);
  194.     fwrite($fp, $content);
  195.     flock($fp, LOCK_UN);
  196.     fclose($fp);
  197.     return true;
  198.    } else {
  199.     return false;
  200.    }
  201. }
  202. //读文件
  203. function safe_file_get_contents($filename)
  204. {
  205.    $fp = @fopen($filename, 'rb');
  206.    if ($fp) {
  207.     flock($fp, LOCK_SH);
  208.     clearstatcache();
  209.     $filesize = filesize($filename);
  210.     if ($filesize > 0) {
  211.      $data = fread($fp, $filesize);
  212.     }
  213.     flock($fp, LOCK_UN);
  214.     fclose($fp);
  215.     return $data;
  216.    } else {
  217.     return false;
  218.    }
  219. }
  220. ?>
  221. <form method="POST">
  222.  <div class="all">
  223.   <div class="head"><?=$title?></div>
  224.   <div class="body">
  225.    <?php
  226.    //计算总票数
  227.    $txt = safe_file_get_contents($filename);
  228.    //初始化
  229.    $thisvote = array();
  230.    $count = count($content);   
  231.    for($i=0; $i<=$count;$i++){
  232.     $thisvote[$i]=0;
  233.    }
  234.    if(!empty($txt)){
  235.     //取出数组
  236.     $txt = json_decode($txt);
  237.     $txt = object_array($txt);
  238.     $my_votes = $txt['votes'];
  239.     
  240.     //投票总数
  241.     for($i=0; $i<count($my_votes); $i++){
  242.      $temp = $my_votes[$i]['vote'];
  243.      $thisvote[$temp]++;
  244.     }
  245.    }
  246.    ?>
  247.    <?php
  248.     $num = count($content);
  249.     for($i=0; $i<$num; $i++){
  250.      $v=$i+1;
  251.      echo <<<EOT
  252.      <div class="item" onmouseover="mouseover();">
  253.       <input type="$type" id="$v" value="$v" name="myvote">
  254.       <label for="$v">$content[$i]</label>
  255.       </input>
  256.       <div>{$thisvote[$v]}票</div>
  257.      </div>
  258.       
  259. EOT;
  260.      }
  261.    ?>
  262.    
  263.   </div>
  264.   <div class="foot">
  265.   <input type="submit" value="提交投票" <?php if($is_voted){ echo 'disabled'; } ?>>
  266.   <a href="javascript:show_detail();" id="detail">(查看投票人)</a>
  267.   </div>
  268.  </div>
  269.  <div  id="result" <?php if(!$is_voted){ echo 'style="display:none;"'; } ?>>
  270.   <div>
  271.   <?php
  272.    if(!empty($txt)){
  273.     //投票详细
  274.     echo '<div id="result_content1">';
  275.     for($j=0; $j<$count; $j++){
  276.      $k=$j+1;
  277.      echo '<div>'."{$k}、{$content[$j]}:";
  278.      if(!empty($my_votes)){
  279.       foreach($my_votes as $a){
  280.        if($a['vote'] == $k){
  281.         echo "<span>{$a['name']}(IP:{$a['ip']})</span>";
  282.        }
  283.       }
  284.      }
  285.      echo '</div>';
  286.     }
  287.     echo '</div>';
  288.    }
  289.   ?>
  290.   </div>
  291.  </div>
  292. </form>
  293.  </BODY>
  294. </HTML>

作者: yyjjyyjj   发布时间: 2010-09-14

作者: wxpcjrjgcs   发布时间: 2010-09-14

算是

作者: zhangmao72   发布时间: 2010-09-14

相关阅读 更多