这篇文章主要介绍了PHP+AJAX 投票器功能,本文通过一个实例代码给大家介绍的非常详细,需要的朋友参考下
终于到AJAX,翻译过来就是”异步Javascript和XML”,他可以实现网页内容的部分加载,可提高用户体验。现在有很多网站都有用这技术,反正你知道他能实现网页的异步更新就差不多了。当然下面的例子都相对简单,并没有体现它这一特点~
投票器
新建文件【 AJAX投票.html】
<html> <head> <script type="text/javascript"> // 这里是js代码 function getVote(int) { if (window.XMLHttpRequest) { // 创建 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) { // 找到 id 为 poll 的控件 document.getElementById('poll').innerHTML = xmlhttp.responseText; } } // 向PHP脚本传递主要参数q xmlhttp.open("GET", "poll_vote.php?q=" + int, true); xmlhttp.send(); } </script> </head> <body> <p id="poll"> <h3>你喜欢吃吗?</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> </p> </body> </html>
创建【poll_vote.php】脚本文件
<?php // 接收参数q $vote = htmlspecialchars($_REQUEST['q']); // 获取文件中存储的数据(这里需要在同一目录下新建一个poll_result.txt文件) $filename = "poll_result.txt"; $conn = file($filename); // 将数据分割到数组 $array = explode("||", $conn[0]); $yes = $array[0]; $no = $array[1]; $count = $array[2]; if ($vote == 0) { $yes += 1; $count += 1; } if ($vote == 1) { $no += 1; $count += 1; } // 将投票数据保存到文档 $insertvote = $yes . '||' . $no . '||' . $count; $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 / ($yes + $no), 2);?>px; height: 20px;"></span><?php echo 100 * round($yes / ($yes + $no), 2); ?>% </td> </tr> <tr> <td>否:</td> <td> <span style="display: inline-block; background-color: red; width: <?php echo 100 * round($no / ($yes + $no), 2);?>px; height: 20px;"></span><?php echo 100 * round($no / ($yes + $no), 2); ?>% </td> </tr> </table> <p><?php echo "参与人数:" . $count; ?></p>
新建一个空白的文档 【poll_result.txt】
此时目录:
|-AJAX投票.html
|-poll_vote.php
|-poll_result.txt
如果不同则需修改上面相应的代码
效果:
相关推荐:
以上是PHP+AJAX 如何实现投票器功能的详细内容。更多信息请关注PHP中文网其他相关文章!

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保themailfunctionisenabled.2)useabasicscruct

发送电子邮件的最佳方法是使用PHPMailer库。1)使用mail()函数简单但不可靠,可能导致邮件进入垃圾邮件或无法送达。2)PHPMailer提供更好的控制和可靠性,支持HTML邮件、附件和SMTP认证。3)确保正确配置SMTP设置并使用加密(如STARTTLS或SSL/TLS)以增强安全性。4)对于大量邮件,考虑使用邮件队列系统来优化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP发送邮件可以通过PHPMailer库实现。1)安装并配置PHPMailer,2)设置SMTP服务器细节,3)定义邮件内容,4)发送邮件并处理错误。使用此方法可以确保邮件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具