有一個great article posted by CSS-Tricks.com描述瞭如何使用PHP和MySQL數據庫創建民意調查。我跟着這個,爲自己創造了一個很好的民意調查。我注意到在評論中提到使用AJAX在同一頁面上顯示結果,而不是完全獨立的頁面。在同一頁面上進行的PHP民意調查與結果
我想知道什麼是在同一頁面上顯示PHP民意調查結果的最佳方式?
更新:
答案很簡單。事實上,在我看來,沒有AJAX的CSS-Tricks調查比較困難,因爲它需要一個數據庫。這個不是!
創建PHP和AJAX投票方式表決的完整的教程可以在這裏看到:
http://www.w3schools.com/php/php_ajax_poll.asp
我只是想澄清如何設置陣列了兩個多民意調查的選項。首先你得到「數據庫」(即文本文件,而不是MySql)。
//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 multiple options
$array = explode("||", $content[0]);
$option1array = $array[0]; //note: these values can be text values also. If text value, nothing changes with this part of the code.
$option2array = $array[1];
$option3array = $array[2];
$option4array = $array[3];
的數據存儲在 「數據庫」
if ($vote == 'option1')
{
$option1array = $option1array + 1;
}
if ($vote == 'option2')
{
$option2array = $option2array + 1;
}
if ($vote == 'option3')
{
$option3array = $option3array + 1;
}
if ($vote == 'option4')
{
$option4array = $option4array + 1;
}
然後,輸出結果。對於文件結構和AJAX腳本,see the complete tutorial.
嗯,似乎選項一更容易,但我感覺你更喜歡選項二。爲什麼? –
我會使用選項一,但因爲在你的問題中,你說有人已經推薦AJAX,好像你正在尋找另一種解決方案。選項2中有一些漏洞,就像另一個人在您進入頁面並提交投票表決之間進行投票時一樣,您也不會在結果中看到該投票。 – Pitchinnate
這不是我正在尋找超越AJAX的選項,而只是我不知道如何使用AJAX實現輪詢來實現相同的頁面結果。 –