Click to See Complete Forum and Search --> : Creating A Poll


BarraGT
08-27-2005, 04:15 AM
I'd like to have a basic poll system for my website.

I'd like it to show the question, show a few options, and when the option is selected and "Vote" is clicked, it will show the results. The results can come up in a new window, if that's easier.

Can anyone point me in the right direction, in terms of a script or something that does this?

Stephen Philbin
08-27-2005, 04:38 AM
You need a server side programming language and preferably a database. Do you have them?

BarraGT
08-27-2005, 04:40 AM
Sure do. The server I am using supports most things.

I'm using PHP for other things at the moment, so if it can be done using that, it'd be great.

Stephen Philbin
08-27-2005, 06:31 AM
That should be easy enough then. Just make a form with a radio box for each poll option, then just have the recieving php script check which one was selected and increment the number in the column for that option by one.

aznchong91
08-27-2005, 09:14 AM
You can see a very simple one I made atVote (http://amo.cheesy.ca/Chong/vote.php) By the way, it's just one page. It took me awhile, but I got it!

BarraGT
09-07-2005, 03:58 AM
Thanks for that. It doesn't really show me how to script it up, though.

I'm specifically looking for a script that will do what I listed in my first post. Any ideas?

aznchong91
09-07-2005, 04:03 AM
I could pm you my script...it's all one page.

BarraGT
09-07-2005, 04:10 AM
That would be fantastic! I'd really appreciate that!

aznchong91
09-08-2005, 03:24 AM
Actually, I'll just put it here(it's long):

<?
////Vote, started 28/7/05.////
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>---Vote---</title>
</head>

<body>
<h1><div align="center">---Vote---</div></h1>

<br>
<?
//Connect...
$dbcnx=mysql_connect("localhost","username","password");
mysql_select_db("your_db", $dbcnx);

//Get all variables neccesary
$Ans1=mysql_result(mysql_query("select Ans1 from Vote_Answers", $dbcnx),0);
$Ans2=mysql_result(mysql_query("select Ans2 from Vote_Answers", $dbcnx),0);
$Ans3=mysql_result(mysql_query("select Ans3 from Vote_Answers", $dbcnx),0);
$Ans4=mysql_result(mysql_query("select Ans4 from Vote_Answers", $dbcnx),0);
$Ans5=mysql_result(mysql_query("select Ans5 from Vote_Answers", $dbcnx),0);
$Question=mysql_result(mysql_query("select Question from Vote_Votes", $dbcnx),0);
$Ans1n=mysql_result(mysql_query("select Ans1 from Vote_Votes", $dbcnx),0);
$Ans2n=mysql_result(mysql_query("select Ans2 from Vote_Votes", $dbcnx),0);
$Ans3n=mysql_result(mysql_query("select Ans3 from Vote_Votes", $dbcnx),0);
$Ans4n=mysql_result(mysql_query("select Ans4 from Vote_Votes", $dbcnx),0);
$Ans5n=mysql_result(mysql_query("select Ans5 from Vote_Votes", $dbcnx),0);
$ip=$_SERVER['REMOTE_ADDR'];
$NOP_query=mysql_query("select NOP from Vote_NOP", $dbcnx) or die(mysql_error());
$NOP=mysql_result($NOP_query, 0);



//Check if you have voted before and process votes
if (isset($_GET[vote]))
{
$blah=1;
while($blah<=$NOP)
{
if (mysql_result(mysql_query("select IP from Vote_IP where ID='$blah'", $dbcnx),0)==$ip)
{
echo"<h3>You have already voted!!! Stop trying to vote again!!!</h3>";
exit();}
$blah++;
}
if ($_GET[vote]=='Ans1')
{
$ansnew=$Ans1n+1;
mysql_query("update Vote_Votes set Ans1='$ansnew'", $dbcnx);

}
elseif ($_GET[vote]=='Ans2')
{

$ansnew=$Ans2n+1;
mysql_query("update Vote_Votes set Ans2='$ansnew'", $dbcnx);

}
elseif ($_GET[vote]=='Ans3')
{

$ansnew=$Ans3n+1;
mysql_query("update Vote_Votes set Ans3='$ansnew'", $dbcnx);

}
elseif ($_GET[vote]=='Ans4')
{

$ansnew=$Ans4n+1;
mysql_query("update Vote_Votes set Ans4='$ansnew'", $dbcnx);

}
elseif ($_GET[vote]=='Ans5')
{

$ansnew=$Ans5n+1;
mysql_query("update Vote_Votes set Ans5='$ansnew'", $dbcnx);

}



mysql_query("insert into Vote_IP values('','$ip')", $dbcnx);
mysql_query("update Vote_NOP set NOP=NOP+1", $dbcnx);
echo"Thank you for voting!<br><br>Results:";
//Display results

?>
<table width="335" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="34">&nbsp;</td>
<td width="295"><? echo "Question: ".$Question;?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans1." : 0 votes, 0%";}
else{echo $Ans1." : ".$Ans1n." votes, ".(($Ans1n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans2." : 0 votes, 0%";}
else{echo $Ans2." : ".$Ans2n." votes, ".(($Ans2n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans3." : 0 votes, 0%";}
else{echo $Ans3." : ".$Ans3n." votes, ".(($Ans3n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans4." : 0 votes, 0%";}
else{echo $Ans4." : ".$Ans4n." votes, ".(($Ans4n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans5." : 0 votes, 0%";}
else{
echo $Ans5." : ".$Ans5n." votes, ".(($Ans5n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
</table>
<?php
}

else{
//Check if you have voted or not
$ip=$_SERVER['REMOTE_ADDR'];
$blah=1;
while($blah<=$NOP)
{
if (mysql_result(mysql_query("select IP from Vote_IP where ID='$blah'", $dbcnx),0)==$ip)
{
echo"<h3>You have already voted.</h3>";
//Show results
?>
<table width="335" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="34">&nbsp;</td>
<td width="295"><? echo "Question: ".$Question;?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans1." : 0 votes, 0%";}
else{echo $Ans1." : ".$Ans1n." votes, ".(($Ans1n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans2." : 0 votes, 0%";}
else{echo $Ans2." : ".$Ans2n." votes, ".(($Ans2n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans3." : 0 votes, 0%";}
else{echo $Ans3." : ".$Ans3n." votes, ".(($Ans3n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans4." : 0 votes, 0%";}
else{echo $Ans4." : ".$Ans4n." votes, ".(($Ans4n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><? if ($NOP==0){
echo $Ans5." : 0 votes, 0%";}
else{
echo $Ans5." : ".$Ans5n." votes, ".(($Ans5n/$NOP)*100)."%";}?>&nbsp;</td>
</tr>
</table>
<?
exit();
}
$blah++;
}
//Vote...
?>

<form name="form1" method="get" action="vote.php">
<table width="337" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="34">&nbsp;</td>
<td width="297"><? echo "Question: ".$Question ;?>&nbsp;</td>
</tr>
<tr>
<td><input name="vote" type="radio" value="Ans1"></td>
<td><? echo $Ans1;?>&nbsp;</td>
</tr>
<tr>
<td><input name="vote" type="radio" value="Ans2"></td>
<td><? echo $Ans2;?>&nbsp;</td>
</tr>
<tr>
<td><input name="vote" type="radio" value="Ans3"></td>
<td><? echo $Ans3;?>&nbsp;</td>
</tr>
<tr>
<td><input name="vote" type="radio" value="Ans4"></td>
<td><? echo $Ans4;?>&nbsp;</td>
</tr>
<tr>
<td><input name="vote" type="radio" value="Ans5"></td>
<td><? echo $Ans5;?>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit_vote" type="submit" value="Vote!">&nbsp;</td>
</tr>
</table>
</form>
<?
}

?>

</body>
</html>


The tables are:
1 (Vote_Answers):
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Ans1 | text | YES | | NULL | |
| Ans2 | text | YES | | NULL | |
| Ans3 | text | YES | | NULL | |
| Ans4 | text | YES | | NULL | |
| Ans5 | text | YES | | NULL | |
+-------+------+------+-----+---------+-------+
2 (Vote_Votes):
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| Question | text | YES | | NULL | |
| Ans1 | int(11) | YES | | NULL | |
| Ans2 | int(11) | YES | | NULL | |
| Ans3 | int(11) | YES | | NULL | |
| Ans4 | int(11) | YES | | NULL | |
| Ans5 | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
3 (Vote_IP):
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| ID | int(11) | | PRI | NULL | auto_increment |
| IP | text | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
4 (Vote_NOP):
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| ID | int(11) | | PRI | NULL | auto_increment |
| NOP | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+