?3.php
<body>
<form action="3.1.php" method="post">
<h3>最美大学生投票系统</h3>
<table width="200" border="1">
<tr>
<td>小诺</td>
<td><input type="radio" name="a" value="小诺"></td>
</tr>
<tr>
<td>老诺</td>
<td><input type="radio" name="a" value="老诺"></td>
</tr>
<tr>
<td>小徐</td>
<td><input type="radio" name="a" value="小徐"></td>
</tr>
<tr>
<td>老徐</td>
<td><input type="radio" name="a" value="老徐"></td>
</tr>
<tr>
<td>老牛</td>
<td><input type="radio" name="a" value="老牛"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="b" value="投票"></td>
</tr>
</table>
</form>
</body>
?效果:
?3.1.php
<body>
<?php
$votefile = "vote.txt";
if (!file_exists($votefile)) {
$handle = fopen($votefile, "w+");
fwrite($handle, "0|0|0");
fclose($handle);
}
if (isset($_POST['b'])) {
if (isset($_POST['a'])) {
$vote = $_POST['a'];
$handle = fopen($votefile, "r+");
$votestr = fread($handle, filesize($votefile));
fclose($handle);
$votearray = explode("|", $votestr);
//var_dump($votearray);
echo "<h3>投票完毕</h3>";
if ($vote == '小诺')
$votearray[0]++;
if ($vote == '老诺')
$votearray[1]++;
if ($vote == '小徐')
$votearray[2]++;
if ($vote == '老徐')
$votearray[3]++;
if ($vote == '老牛')
$votearray[4]++;
echo "目前小诺的支持票数为:" . $votearray[0] . "<br>";
echo "目前老诺的支持票数为:" . $votearray[1] . "<br>";
echo "目前小徐的支持票数为:" . $votearray[2] . "<br>";
echo "目前老徐的支持票数为:" . $votearray[3] . "<br>";
echo "目前老牛的支持票数为:" . $votearray[4] . "<br>";
$sum = $votearray[0] + $votearray[1] + $votearray[2]+ $votearray[3]+ $votearray[4];
echo "总票数为:" . $sum . "<br>";
$votestr2 = implode("|", $votearray);
$handle = fopen($votefile, "w+");
fwrite($handle, $votestr2);
fclose($handle); }
else {echo "<script>alert('未选择投票选项!');
history.back();</script>";
} }
?>
</body>
效果:
?自动在根目录下生成vote.txt文件
?scc样式
<style type="text/css">
table {
width: 400px;
background: #ccc;
margin: 10px auto;
border-collapse: collapse;/*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/
}
th,td {
height: 25px;
line-height: 25px;
text-align: center;
border: 1px solid #ccc;
}
th {
background: #eee;
font-weight: normal;
}
tr {
background: #ccc;
}
tr:hover {
background: #cc0;
}
body {
background:#FCC;
color: #fff;
text-shadow: 0 1px 0 #ccc, 0 2px 0 #c9c9c9, 0 3px 0 #bbb, 0 4px 0 #b9b9b9, 0 5px 0 #aaa, 0 6px 1px rgba(0,0,0,0.1), 0 0 5px rgba(0,0,0,0.1),0 1px 3px rgba(0,0,0,0.3),0 3px 5px rgba(0,0,0,0.2),0 5px 10px rgba(0,0,0,0.25);
}
h3{text-align:center}
</style>
|