Jump to content

php help (voting) ?


jd2007

Recommended Posts

this is the voting form:

 

<?php
echo "<form method='post' action='voting2.php'>
      Who do you think will be the next President of the United States ?<br>
      <input type='radio' name='NextPresident' value='Barack Obama' checked='checked' />Barack Obama<br>
      <input type='radio' name='NextPresident' value='Hilary Clinton' />Hilary Clinton<br> 
      <input type='submit' value='Vote' />
      </form>";
?>

 

voting2.php is :

 

<?php

$obama=0;
$clinton=0;
$time=getdate();

if (!$time[month]="August" && !$time[mday]=14 && !$time[year]=2007) {
echo "Thank you.";
if ($_POST["NextPresident"]=="Barack Obama") {
    $obama++;
}
else {
$clinton++;
}

}
else if ($time[month]="July" && $time[mday]=14 && $time[year]=2007) {

$total=$obama+$clinton;
$obama_perc=($obama /  $total) * 100;
$clinton_perc=($clinton /  $total) * 100;
echo "<img src='barack.png' width='$obama_perc' height='20'><br><br><img src='clinton.png' width='$clinton_perc' height='20'>";
}

else {}

?>

 

barack.png and clinton.png is a purple bar which acts as a graph bar. this thing i'm doing is a poll until august 14, 2007. users vote whether Barack Obama or Hilary Clinton will be President. after august 14, 2007, it does some calculation and controls barack.png and clinton.png size, so that it acts like a graph...pls check why its not working...pls help me ?

Link to comment
https://forums.phpfreaks.com/topic/59816-php-help-voting/
Share on other sites

I modified your code to get around your current problem, however I am still unsure how you plan to keep track of the votes and also keep track of who has voted already. I suggest a mysql database to keep track of the votes and to use sessions/cookies to keep track of people that have already voted.

 

test_form.php

<?php
echo "<form method='post' action='voting2.php'>
      Who do you think will be the next President of the United States ?<br>
      <input type='radio' name='NextPresident' value='Barack Obama' checked='checked' />Barack Obama<br>
      <input type='radio' name='NextPresident' value='Hilary Clinton' />Hilary Clinton<br> 
      <input type='submit' name='login' value='Vote' />
      </form>";
?>

 

voting2.php

<?php

if (isset($_POST['login']))
{
    $obama = 0;
    $clinton = 0;
    $current_date = date("M-d-Y", mktime());
    $end_date = date("M-d-Y", mktime(0, 0, 0, 8, 14, 2007));

    if ($current_date != $end_date)
    {
        echo "Thank you.<br />";

        if ($_POST["NextPresident"] == "Barack Obama")
        {
            $obama++;
        }
        else
        {
            $clinton++;
        }

        $total = $obama+$clinton;

        if ($obama == 0)
        {
            echo "<img src='barack.png' width='0' height='20'>";
        }
        else
        {
            $obama_perc=($obama /  $total) * 100;
            echo "<img src='barack.png' width='$obama_perc' height='20'><br><br>";
        }
        if ($clinton == 0)
        {
            echo "<img src='clinton.png' width='0' height='20'>";
        }
        else
        {
            $clinton_perc=($clinton /  $total) * 100;
            echo "<img src='clinton.png' width='$clinton_perc' height='20'>";
        }
    }
    else
    {
        echo "Voting has ended!";
    }
}
else
{
    echo "You must vote first!";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/59816-php-help-voting/#findComment-297447
Share on other sites

I think that the best way of keeping the trak of all the people who voted is to get their ip and store it because (at least me) people delete their cookies after using the browser.

About the code. Not sure what's the problem but maybe you should try doing something like this:

echo "<img src='barack.png' width='".$obama_perc."' height='20'><br><br>";

 

instead of this:

 

echo "<img src='barack.png' width='$obama_perc' height='20'><br><br>";

Link to comment
https://forums.phpfreaks.com/topic/59816-php-help-voting/#findComment-297463
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.