《PHP實(shí)例:PHP+AJAX實(shí)現(xiàn)投票功能的方法》要點(diǎn):
本文介紹了PHP實(shí)例:PHP+AJAX實(shí)現(xiàn)投票功能的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用本文實(shí)例講述了PHP+AJAX實(shí)現(xiàn)投票功能的辦法.分享給大家供大家參考.具體如下:
PHP應(yīng)用在這個(gè) AJAX 實(shí)例中,我們將演示一個(gè)投票程序,網(wǎng)頁在不重新加載的情況下,就可以獲得結(jié)果.
PHP應(yīng)用本例包括四個(gè)元素:
PHP應(yīng)用① HTML 表單
② JavaScript
③ PHP 頁面
④ 存放結(jié)果的文本文件
PHP應(yīng)用一、HTML 表單
PHP應(yīng)用這是 HTML 頁面.它包含一個(gè)簡(jiǎn)單的 HTML 表單,以及一個(gè)與 JavaScript 文件的連接:
PHP應(yīng)用
<html>
<head>
<script src="poll.js"></script>
</head>
<body>
<div id="poll">
<h2>Do you like PHP and AJAX so far?</h2>
<form>
Yes:
<input type="radio" name="vote"
value="0" onclick="getVote(this.value)">
<br />
No:
<input type="radio" name="vote"
value="1" onclick="getVote(this.value)">
</form>
</div>
</body>
</html>
PHP應(yīng)用例子解釋 - HTML 表單
PHP應(yīng)用正如您看到的,上面的 HTML 頁面包含一個(gè)簡(jiǎn)單的 HTML 表單,其中的 <div> 元素帶有兩個(gè)單選按鈕.
PHP應(yīng)用表單這樣工作:
PHP應(yīng)用1. 當(dāng)用戶選擇 "yes" 或 "no" 時(shí),會(huì)觸發(fā)一個(gè)事件
2. 當(dāng)事件觸發(fā)時(shí),執(zhí)行 getVote() 函數(shù)
3. 圍繞該表單的是名為 "poll" 的 <div>.當(dāng)數(shù)據(jù)從 getVote() 函數(shù)返回時(shí),返回的數(shù)據(jù)會(huì)替代該表單.
PHP應(yīng)用二、文本文件
PHP應(yīng)用文本文件 (poll_result.txt) 中存儲(chǔ)來自投票程序的數(shù)據(jù).
PHP應(yīng)用它類似這樣:
PHP應(yīng)用0||0
第一個(gè)數(shù)字表示 "Yes" 投票,第二個(gè)數(shù)字表示 "No" 投票.
PHP應(yīng)用注釋:記得只允許您的 web 服務(wù)器來編輯該文本文件.不要讓其他人獲得拜訪權(quán),除了 web 服務(wù)器 (PHP).
PHP應(yīng)用三、JavaScript
PHP應(yīng)用JavaScript 代碼存儲(chǔ)在 "poll.js" 中,并于 HTML 文檔相連接:
PHP應(yīng)用
var xmlHttp
function getVote(int)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="poll_vote.php"
url=url+"?vote="+int
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("poll").
innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
PHP應(yīng)用例子解釋:
PHP應(yīng)用stateChanged() 和 GetXmlHttpObject 函數(shù)與 PHP 和 AJAX 哀求 這一節(jié)中的例子相同.
PHP應(yīng)用getVote() 函數(shù)
PHP應(yīng)用當(dāng)用戶在 HTML 表單中選擇 "yes" 或 "no" 時(shí),該函數(shù)就會(huì)執(zhí)行.
PHP應(yīng)用1. 定義發(fā)送到服務(wù)器的 url (文件名)
2. 向 url 添加參數(shù) (vote),參數(shù)中帶有輸入字段的內(nèi)容
3. 添加一個(gè)隨機(jī)數(shù),以防止服務(wù)器使用緩存的文件
4. 調(diào)用 GetXmlHttpObject 函數(shù)來創(chuàng)建 XMLHTTP 對(duì)象,并告知該對(duì)象當(dāng)觸發(fā)一個(gè)變化時(shí)執(zhí)行 stateChanged 函數(shù)
5. 用給定的 url 來打開 XMLHTTP 對(duì)象
6. 向服務(wù)器發(fā)送 HTTP 哀求
PHP應(yīng)用四、PHP頁面
PHP應(yīng)用由 JavaScript 代碼調(diào)用的服務(wù)器頁面是名為 "poll_vote.php" 的一個(gè)簡(jiǎn)單的 PHP 文件.
PHP應(yīng)用
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0)
{
$yes = $yes + 1;
}
if ($vote == 1)
{
$no = $no + 1;
}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
PHP應(yīng)用例子解釋:
PHP應(yīng)用所選的值從 JavaScript 傳來,然后會(huì)發(fā)生:
PHP應(yīng)用1. 獲取 "poll_result.txt" 文件的內(nèi)容
2. 把文件內(nèi)容放入變量,并向被選變量累加 1
3. 把結(jié)果寫入 "poll_result.txt" 文件
4. 輸出圖形化的投票結(jié)果
PHP應(yīng)用希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所贊助.
歡迎參與《PHP實(shí)例:PHP+AJAX實(shí)現(xiàn)投票功能的方法》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/8639.html