PHP-Switch statement with form radio buttons?

penguinluvinman

New member
I'm trying to make a poll on a website, and handle the users choice with a switch statement. My code is listed below. It is supposed to check and make sure they haven't voted by seeing if the cookie has been stored, as well as storing their IP in a database after they vote. I've played around with some stuff and figured out that it is not getting into the IF $MODE=VOTED part at all although after you vote the url changes to "page.php?vote=2&mode=voted"

Can someone please let me know what I'm doing wrong?

HTML form:
Does this poll work?
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<input type="radio" name="vote" value="1"> Yes<br>
<input type="radio" name="vote" value="2"> No<br>
<input type="radio" name="vote" value="3"> I hope so<br>
<input type="hidden" name="mode" value="voted">
<input type="submit" value="Vote!">
</form>


PHP handler:
if ($mode=="voted")
{

//makes sure they haven't already voted
if(isset($_COOKIE[$cookie]) || (mysql_num_rows($alreadyvotedquery) > 0))
{
echo "Sorry you have already voted in this poll.<br>";
}

//sets a cookie
else
{
$year = 60*60*24*31*12 + time();
setcookie(Voted,Voted, $year);
// adds ip to database
$addipquery = mysql_query("INSERT INTO `responses` (`pid`, `ip`) VALUES ('1', '".$_SERVER['REMOTE_ADDR']."')");

switch ($_POST['vote']) {
case 1:
mysql_query ("UPDATE votes SET first = first+1 WHERE pollid='1'");
echo 'Vote successful!';
break;
case 2:
mysql_query ("UPDATE votes SET second = second+1 WHERE pollid='1'");
echo 'Vote successful!';
break;
case 3:
mysql_query ("UPDATE votes SET third = third+1 WHERE pollid='1'");
echo 'Vote successful!';
}
.....
}
 
Back
Top