Creating a simple poll

We are going to create a simple poll that users may select what programming laguage they prefer (ASP or PHP)

First of all prepare four files as below:

1. 'asp.txt' and then insert the number 0 in it as initial value. 
2. 'php.txt' and then type the number 0 in it as initial value. 
Note:  Do not forget to insert the value 0 in the 'asp.txt' and 'php.txt'. This is very important so that the script works
3. create an image file 'poll.jpg' with size 100x10px. This is to make the percentage of polling. 
This is also important to make our poll eye-catching. 
4. 'index.php' and type in it the script below 

<title> Simple Polling </ title>
<style type="text/css">
<! --
. style1 (font-size: 24px)
. style2 (color: # 0000FF)
->
</style>
<form name="form1" method="post" action="index.php?content=polling">
<p>
Polling <label> <span class="style1"> </span> </label>
</p>
<p>
<label> Which web programming language do you prefer? </label>
</p>
<p>
<label>
<input type="radio" name="vote" value="php">
PHP </label>
<br>
<label>
<input type="radio" name="vote" value="asp">
ASP </label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
<br>
</p>
</form>
<?php
if (@$_GET['content'])
{
$vote = $_POST['vote'];
if ($vote == "") // if no choice yet
{
echo "You have not <font color=red> fill surveys </font>";
exit;
}
if ($vote == "php") // add 1 for  php
{
$open = fopen("php.txt", "r");
$read = fgets($open, 65535);
fclose ($open);
$open = fopen("php.txt", "w");
$read++;
fwrite ($open, $read);
fclose ($open);
}
if ($vote == "asp") // add 1 for asp
{
$open = fopen ("asp.txt", "r");
$read = fgets ($open, 65535);
fclose($open);
$open = fopen("asp.txt", "w");
$read++;
fwrite($open, $read);
fclose($open);
}
$open_php = fopen("php.txt", "r");
$readphp = fgets($open_php, 65535); // read the value for php
fclose($open_php);
$open_asp = fopen ("asp.txt", "r");
$readasp = fgets($open_asp, 65535); // read the value for asp
fclose($open_asp);
$total_user = $readphp + $readasp; // the number of voters
$persentase_php = ($readphp / $total_user) * 100;
$persentase_asp = ($readasp / $total_user * 100);
echo "Total voters: $total_user <br>";
echo "PHP:"; printf("% 1.0f", "$persentase_php"); echo "% <img src=poll.jpg width =$persentase_php  $height = 10 /> $readphp voters <br> ";
echo "ASP"; printf("% 1.0f", "$persentase_asp"); echo "% <img src = poll.jpg width =$persentase_asp  $height = 10 /> $readasp voters <br> ";
}
?>

No comments: