how to stop auto updating

Busy's picture

He has: 6,151 posts

Joined: May 2001

I have a basic voting form but I can't stop it updating on refresh of the browser.
without the bloat of the code, I've got something like this:

get results from db
if rated == "" display voting options and previous details
on vote - add new stuff to old stuff and update db
if rated != "" dont display vote options and show new details (from previous results, not from db)

'rated' is a hidden form input. have also tried if ($REQUEST_METHOD=='POST')

this all works fine but after the vote, hitting refresh keeps updating the new figures.

any ideas?

He has: 1,016 posts

Joined: May 2002

You need to store some info to know who has voted and who has not. The way I see it, you have 3 options...

1. Set a cookie after the users votes.
2. Use sessions to do the same thing.
3. Store the IP in a table in the database.

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks but that has 3 problems,
1. cookies can be disabled (unaccepted) I started using cookiewall and pick and choose which cookies to accept or deny, adaware has had nothing to do since I got it.
2. I'm using sessions in the page but I need to empty the session after each page view (and can be over 2000), so voting, going forward one page then back would be same as refreshing as the session wouldn't be valid.
3. similar to number 2, also not 100% sure how I could do it, as it would need to be flushed after each visit (browser) so they could return the next day I suppose.

Sessions and cookies (among others) aren't my strong points, how can I use then seperate them, without my code looking like the draft of war and peace. at the moment I already have about 6 or 8 session variables registered per display (each page)

I wish there was something that would easily let me do something like this:

You can do this as you havent done it before
If you did this, you can't do this again
umm your naughty you tryed to do it again

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

The only fool proof way to have an accurate poll is to have user regisration.

That's kinda why you have to register to vote around here too. Wink

They have: 17 posts

Joined: Jan 2002

Hi

zollet, told you how to do it......

example......

<?php
$ip
= \"$HTTP_SERVER_VARS[REMOTE_HOST]\";

$total = get_voted($poll, $ip, $db);

if (
$total > 0) {

// redirect to resluts page or give error

echo \"<td>Already Voted</td>\";

} else {

// they have not voted, show the vote button

echo \"<td>INPUT VOTE BUTTON</td>

}


// the function............

function get_voted(
$poll, $ip, $db) {

      mysql_select_db(
$db);

     
$sql = \"SELECT count(*)AS total FROM voted WHERE poll_id = '$poll' AND ip = '$ip'\";
   if(!
$result = mysql_query($sql)) {
     return(\"ERROR\");
    }
   if(!
$myrow = mysql_fetch_array($result)) {
     return(\"0\");
    } else {
   return(
$myrow['total']);
   }
}
?>

F!

They have: 447 posts

Joined: Oct 1999

if all you want to do is stop refreshing from re-submitting the form, just forward them back to the page after a vote...

example:

<?php
//when someone votes
//process the vote here
//then forward them
header(\"Location: yourpage.php\"); exit;
?>

Busy's picture

He has: 6,151 posts

Joined: May 2001

prevote and postvote are the same page

They have: 447 posts

Joined: Oct 1999

yes, but if you forward them back (to the same page theyre on) the form is reset, and data will no longer be submitted on subsequent refreshes.

Busy's picture

He has: 6,151 posts

Joined: May 2001

the data wouldn't be submitted but the form would be open for another vote which is kind of like refreshing.

Thanks anyway

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.