這篇文章主要介紹了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 id="你喜欢吃吗">你喜欢吃吗?</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 id="结果">结果:</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中文網其他相關文章!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。