How To Create Simple Polling / Voting Menu With PHP Script

Share:

How To Create Simple Polling / Voting Menu With PHP Script. To make the website more interactive, usually most of the website will display a poll / vote. Poll / vote can have many benefits, among others can be used for polling / voting of the impression of our website.
How To Create Simple Polling / Voting Menu With PHP ScriptActually, the Internet has many websites which provide php script for polling / voting for free without the need to create a php script yourself hard and you just upload and install it on the website. However, to broaden our horizons about php programming, very good if we know how to make a simple poll / vote for our website. In this tutorial, we will discuss about how to create a simple poll / vote using php. The following tutorial :

Download Now How To Create Simple Polling / Voting Menu With PHP Script

How To Create Simple Polling / Voting Menu With PHP Script

Create .Txt File
Just like the previous tutorial to make page view counter, here we also need a txt file to store the results of the poll / voting. In this first step, we need to create a txt file with the following contents
poll-vote.txt

How do you think about Tutorial.World.Edu blog? &Excellent;Good;Not Bad;Bad &0;0;0;0;

Note: You can replace the contents of the poll / vote in accordance with your wishes.

Create PHP Polling / Voting Script
The next step is to create a php script that menu poll / vote can be processed. Make sure you have activated the php fopen function on your website hosting because in create this simple poll / vote, we will use it. Here is the complete php script :
poll-vote-script.php

<?php
session_start();
if(file_exists("poll-vote.txt")) {
$file = fopen("poll-vote.txt",'r');
$poll = fgets($file,4000);
fclose($file);
$data = explode("&",$poll);
$choice = explode(";",$data[1]);
$counter = explode(";",$data[2]);
if (!$_SESSION['status']) {
$_SESSION['status']="choice";
$counter[$opt]++;
$file = fopen("poll-vote.txt",'w');
$poll = $data[0]."+".$data[1]."+";
for($i = 0;$i < count($choice);$i++) {
$poll .= $counter[$i].";";
}
fputs($file,$poll);
fclose($file);
}
echo $data[0],"<br>Your choise $choice[$opt]<br>";
for($i = 0;$i < count($choice); $i++){
$jml=$jml+$counter[$i];
}
echo "<table>
<tr>
<th>Result</th>
<th>Percentage</th>
<th></th>
<th>Vote</th>
</tr>";
for($i = 0;$i < count($choice); $i++){
if($jml==0){
$j=1;
}else{
$j=$jml;
}
$result_vote=sprintf("%01.1f",(($counter[$i]/$j)*100));
echo "<tr>
<td>$choice[$i]</td>
<td>
<table>
<tr>
<td bgcolor=\"$choice[$i]\" width=$result_vote>&nbsp;</td>
</tr>
</table>
</td>
<td>$result_vote %</td>
<td align=center>$counter[$i]</td>
</tr>";
}
}
?>

Create Simple Polling / Voting Form
The final step is to make a poll / vote form. Immediately, the following script:
poll-vote-form.php

<?php
if(file_exists("poll-vote.txt")) {
echo "<form method=post action=\"poll-vote-script.php\">";
$file = fopen("poll-vote.txt",'r');
$poll = fgets($file,5000);
fclose($file);
$data = explode("&",$poll);
echo $data[0]."<br>";
$choice = explode(";",$data[1]);
for($i = 0;$i < count($choice);$i++){
if($i == 0){
echo "<input type=radio name=opt value=$i checked>$choice[$i]<br>";
}else{
echo "<input type=radio name=opt value=$i>$choice[$i]<br>";
}
}
echo "<br><input type=submit value=Vote>";
echo "</form>";
} else {
echo "There are no poll / vote";
}
?>

Now you can try it on localhost or directly uploaded into the website hosting for get the results. Good luck …
Thanks for reading How To Create Simple Polling / Voting Menu With PHP Script

[Tutorial.World.Edu]

3 thoughts on “How To Create Simple Polling / Voting Menu With PHP Script

Leave a Reply

Your email address will not be published. Required fields are marked *