一个简单的投票功能
html代码:
<html> <head> </head> <body> <h3 id='toupiao'>请选择</h3> <div id="div"> 1:<input type="radio" name="vote" value="0" onclick="getpiao(this.value)"><br> 2:<input type="radio" name="vote" value="1" onclick="getpiao(this.value)"></div> <table style='display:none' id="table"> <tr> <td>选1:</td> <td> <input style=" height:20px;background-color:red" value="" id="yes"/>% </td> </tr> <tr> <td>选2:</td> <td> <input style=" height:20px;background-color:blue" value="" id="no">% </td> </tr> </table> <script type="text/javascript" src="jquery-1.8.1.min.js"></script> <script> function getpiao(int) { $.post("/toupiao/piao.php",{"piao":int},function(data){ if(data.yes!="" || data.no!=""){ var newno = parseInt(data.no); var newyes = parseInt(data.yes); $('#yes').val((newyes/(newno+newyes)*100).toFixed(2)); $('#yes').css('width', (newyes/(newno+newyes)*100).toFixed(2)+'px'); $('#no').val((newno/(newno+newyes)*100).toFixed(2)); $('#no').css('width', (newno/(newno+newyes)*100).toFixed(2)+'px'); $('#table').css('display',"block"); $('#div').css('display',"none"); $('#toupiao').html('投票结果'); } },"json"); } </script> </body> </html>
PHP代码:
<?php $piao = $_POST['piao']; $filename = "piao.txt"; $content = file($filename); $array = explode("/", $content[0]); $yes = $array[0]; $no = @$array[1]; if ($piao == 0) $yes = $yes + 1; if ($piao == 1) $no = $no + 1; $insertpiao = $yes."/".$no; $fp = fopen($filename,"w"); fputs($fp,$insertpiao); fclose($fp); echo json_encode(array('yes'=>$yes,'no'=>$no)); ?>