Make Simple Polling

11.09.2008
Creating a poll or a poll with a simple code html, php and javascript. To type the code can use Notepad program.

Save the following code with the name poll.js:

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
}
Save the following code with the name poll_form.html code, view this as the first page:








Silahkan pilih kesukaan anda :


Pilihan 1

Pilihan 2

Pilihan 3





Save the following code with the name poll_vote.php, this code to display the results of the voice that appear after we choose one of the options on the first page (the page poll_form.html):
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = “poll_result.txt”;
$content = file($filename);

//put content in array
$array = explode(”||”, $content[0]);
$vote1 = $array[0];
$vote2 = $array[1];
$vote3 = $array[2];

if ($vote == 0)
{
$vote1 = $vote1 + 1;
}
if ($vote == 1)
{
$vote2 = $vote2 + 1;
}
if ($vote == 2)
{
$vote3 = $vote3 + 1;
}

//insert votes to txt file
$insertvote = $vote1.”||”.$vote2.”||”.$vote3;
$fp = fopen($filename,”w”);
fputs($fp,$insertvote);
fclose($fp);
?>






















Hasil:
Pilihan 1
’ height=’10′>
%
Pilihan 2
’ height=’10′>
%
Pilihan 3
’ height=’10′>
%
Total Suara:

Once the code is written above, save the third file the same location. You can do so locally or try the web hosting that you have. Open the file poll_form.html as the main page of a poll or a poll by your browser. The results of a poll or polls will be stored in the form of a text file as follows: 6 | | 0 | | 0.
You can modify the option to add a poll or a poll, the file poll_form.html and poll_vote.php (see the pattern, adding options such as Options 3
Source: modification of w3schools.com/php /

0 komentar:

Posting Komentar