PHP:简单的投票页面,将统计票数保存在本地文件中

<meta http-equiv="Content-type"  Content="text/html;charset=UTF-8;" />
<!DOCTYPE html>
<style type="text/css">
div{font-size:18px;color:#0000FF;}
li{font-size:24px;color:#FF0000;}
</style>

<form action="" method="post">
    <table align="center">
        <tr><td bgcolor="#CCCCCC"><div>当前最流行的web开发语言:</div></td></tr>
        <tr><td><input type="radio" name="vote" value="PHP">PHP</td></tr>
        <tr><td><input type="radio" name="vote" value="ASP">ASP</td></tr>
        <tr><td><input type="radio" name="vote" value="JSP">JSP</td></tr>
        <tr><td><input type="submit" name="sub" value="请投票"></td></tr>
    </table>
</form>

<?php
    $votefile="EX5_2vote.txt";
    if(!file_exists($votefile))
    {
        file_put_contents($votefile,"0|0|0");
    }
    if(isset($_POST['sub']))
    {
        if(isset($_POST['vote']))
        {
            $vote=$_POST['vote'];
            $votestr=file_get_contents($votefile,0);
            $votearray=explode("|",$votestr);
            echo "<center><h3>投票完毕!</h3></center>";
            if($vote=='PHP') $votearray[0]++;
            echo "<center>目前PHP的票数为:<li>".$votearray[0]."</li></center><br>";
            if($vote=='ASP') $votearray[1]++;
            echo "<center>目前SAP的票数为:<li>".$votearray[1]."</li></center><br>";
            if($vote=='JSP') $votearray[2]++;
            echo "<center>目前JSP的票数为:<li>".$votearray[2]."</li></center><br>";

            $sum=$votearray[0]+$votearray[1]+$votearray[2];
            echo "<center>总票数为:<li>".$sum."</li></center><br>";

            $votestr2=implode("|",$votearray);
            file_put_contents($votefile,$votestr2);
        }
        else
        echo "<script>alert('未选择投票选项!')</script>";
    }
?>
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的jsp页面实现您的需求。以下是示例代码: 投票页面(vote.jsp): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>投票</title> </head> <body> <h1>投票</h1> <form action="vote.do" method="post"> <table> <tr> <td>选项1:</td> <td><input type="radio" name="option" value="option1"></td> </tr> <tr> <td>选项2:</td> <td><input type="radio" name="option" value="option2"></td> </tr> <<tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html> ``` 投票处理程序(vote.do): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 验证用户是否登录 Object user = session.getAttribute("user"); if (user == null) { response.sendRedirect("login.jsp"); return; } // 获取用户投票选项 String option = request.getParameter("option"); // 判断选项是否为空 if (option == null || option.trim().isEmpty()) { out.println("<p>请选择一个选项!</p>"); return; } // 获取投票结果 ServletContext application = getServletContext(); synchronized (application) { Integer count = (Integer) application.getAttribute(option); if (count == null) { count = 0; } count++; application.setAttribute(option, count); } // 显示投票结果 response.sendRedirect("result.jsp"); %> ``` 投票结果页面(result.jsp): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 验证用户是否登录 Object user = session.getAttribute("user"); if (user == null) { response.sendRedirect("login.jsp"); return; } // 获取投票结果 ServletContext application = getServletContext(); Integer count1 = (Integer) application.getAttribute("option1"); Integer count2 = (Integer) application.getAttribute("option2"); if (count1 == null) { count1 = 0; } if (count2 == null) { count2 = 0; } // 显示投票结果 %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>投票结果</title> </head> <body> <h1>投票结果</h1> <table> <tr> <td>选项1:</td> <td><%=count1%>票</td> </tr> <tr> <td>选项2:</td> <td><%=count2%>票</td> </tr> </table> <p><a href="vote.jsp">返回投票页面</a></p> </body> </html> ``` 登录页面(login.jsp): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> <form action="login.do" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form> </body> </html> ``` 登录处理程序(login.do): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 获取用户输入的用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 判断用户名和密码是否正确 if ("admin".equals(username) && "123456".equals(password)) { session.setAttribute("user", username); response.sendRedirect("vote.jsp"); } else { out.println("<p>用户名或密码错误!</p>"); } %> ``` 在以上示例代码,我们通过使用session来实现对用户的验证,确保只有已登录的用户才能进行投票操作。同时,我们还使用了application来保存投票结果,确保在不同的用户间共享投票数据。最后,在显示投票结果时,我们使用了include动作,将投票结果页面包含到其他页面。 当然,以上示例代码只是一个简单实现,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值