分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

PHP - AJAX投票

发布时间:2023-09-06 02:28责任编辑:彭小芳关键词:PHP

上代码…
两个文件:poll.html poll_vote.php
poll.html:

<html><head><meta charset="utf-8"><title>ajax投票</title><script>function getVote(int) { ?if (window.XMLHttpRequest) { ???// IE7+, Firefox, Chrome, Opera, Safari 执行代码 ???xmlhttp=new XMLHttpRequest(); ?} else { ???// IE6, IE5 执行代码 ???xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); ?} ?xmlhttp.onreadystatechange=function() { ?if (xmlhttp.readyState==4 && xmlhttp.status==200) ???{ ?????document.getElementById("poll").innerHTML=xmlhttp.responseText; ???} ?} ?xmlhttp.open("GET","poll_vote.php?vote="+int,true); ?xmlhttp.send();}</script></head><body><div id="poll"><h3>你喜欢 PHP 和 AJAX 吗?</h3><form>是:<input type="radio" name="vote" value="0" onclick="getVote(this.value)"><br>否:<input type="radio" name="vote" value="1" onclick="getVote(this.value)"></form></div></body></html>

  

poll_vote.php:

<?php$vote = htmlspecialchars($_REQUEST[‘vote‘]); ??//把预定义的字符转换为 HTML 实体// 获取文件中存储的数据$filename = "poll_result.txt";$content = file($filename);// 将数据分割到数组中$array = explode("||", $content[0]);$yes = $array[0];$no = $array[1];if ($vote == 0){ $yes = $yes + 1;}if ($vote == 1){ $no = $no + 1;}// 插入投票数据$insertvote = $yes."||".$no;$fp = fopen($filename,"w");fputs($fp,$insertvote);fclose($fp);?><h2>结果:</h2><table> <tr> <td>是:</td> <td> <span style="display: inline-block; background-color:green; ????width:<?php echo(100*round($yes/($no+$yes),2)); ?>px; ????height:20px;" ></span> <?php echo(100*round($yes/($no+$yes),2)); ?>% </td> </tr> <tr> <td>否:</td> <td> <span style="display: inline-block; background-color:red; ????width:<?php echo(100*round($no/($no+$yes),2)); ?>px; ????height:20px;"></span> <?php echo(100*round($no/($no+$yes),2)); ?>% </td> </tr></table>

  

PHP - AJAX投票

原文地址:https://www.cnblogs.com/CBDLL/p/10221222.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved