suttercain Posted May 6, 2007 Share Posted May 6, 2007 Hello everyone, I have a small script I wrote so people can vote on a page: <?php //voting system function voted() { echo "Thank you for voting!"; } $voting = "SELECT * FROM comicvote WHERE comic_id ='" . $row['comic_id'] . "'"; $votes = mysql_query($voting) or die (mysql_error()); $sum = 0; while ($rowvote = mysql_fetch_row($votes)) { $sum += $rowvote['0']; } $average = $sum/5 *100; $format = number_format($average,2); if ($votes == !NULL) { if ($average >= 1 && $average <= 59.99) { $rating = "<img src='../images/comics/grade_f.gif'>"; } elseif ($average==0) { $rating = "Be The First To Vote!<br>"; } elseif ($average >= 60.00 && $average <= 69.99) { $rating = "<img src='../images/comics/grade_d.gif'>"; } elseif ($average >= 70.00 && $average <= 79.99) { $rating = "<img src='../images/comics/grade_c.gif'>"; } elseif ($average >= 80.00 && $average <= 89.99) { $rating = "<img src='../images/comics/grade_b.gif'>"; } else { $rating = "<img src='../images/comics/grade_a.gif'>"; } echo $rating; } $total = mysql_num_rows($votes); if ($total > 0) { echo "<br>Based on $total votes!"; } if ($vbulletin->userinfo['usergroupid'] == '6' || $vbulletin->userinfo['usergroupid'] == '2') { if (!isset($_POST['submit'])) { voteNow(); } else { $rating = $_POST['rating']; $comic_id = $row['comic_id']; $sent = mysql_query ("INSERT INTO comicvote (rating, comic_id) VALUES ('$rating', '$comic_id')") or die(mysql_error()); unset($_POST['submit']);//Trying to unset the $_POST, not working though! voted (); } } else { echo "<a href='/forums/register.php?s='><br>Login to Vote</a>"; } function voteNow() { ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <div align="center"> <select name="rating"> <option value="5">A</option> <option value="4">B</option> <option value="3">C</option> <option value="2">D</option> <option value="1">F</option> </select> <input type="submit" name="submit" value="Vote"> </div> </form> <?php } ?> The issue I am having is that after the vote in inserted into the MySQL database I want the $_POST['submit'] to be unset or destroyed. This isn't working, because each time the page is reloaded it votes again and again. Anyone have any suggestions? Thanks. SC Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted May 6, 2007 Share Posted May 6, 2007 redirect the page after it is done processing, else the client will continue to send post headers Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 6, 2007 Author Share Posted May 6, 2007 What if I need the page to be directed to itself? I need it so the user never leaves that page. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted May 6, 2007 Share Posted May 6, 2007 doesnt matter, just redirect it to itself Quote Link to comment Share on other sites More sharing options...
yzerman Posted May 6, 2007 Share Posted May 6, 2007 instead of unsetting $_POST['submit'] before this line: echo "Thank you for voting!"; } add $submitted = $_POST['submit']; if ($submitted == $_POST['submit']) { //do the query and echo the results $submitted = NULL; } //close this if statement then change if (!isset($_POST['submit'])) { to if ($submitted != $_POST['submit']) { That should work - maybe Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 6, 2007 Author Share Posted May 6, 2007 I tried running this, but of course got that the headers have already been sent: [code] <?php if (!isset($_POST['submit'])) { voteNow(); } else { $rating = $_POST['rating']; $comic_id = $row['comic_id']; $sent = mysql_query ("INSERT INTO comicvote (rating, comic_id) VALUES ('$rating', '$comic_id')") or die(mysql_error()); unset($_POST['submit']); voted (); header("comicvote.php"); } ?> ERROR: Warning: Cannot modify header information - headers already sent by (output started at /home/superman/public_html/comics/view_comics1.php:13) in /home/superman/public_html/comics/comicvote.php on line 41 Other than using <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> I am not sure how to do this.[/code] Quote Link to comment Share on other sites More sharing options...
yzerman Posted May 6, 2007 Share Posted May 6, 2007 if your going to do a redirect, use javascript and not a header just change that line to this: echo "<br />Redirecting...<script language='javascript'>setTimeout('window.location=\"./comicvote.php\"',2000);</script>"; Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted May 6, 2007 Share Posted May 6, 2007 Or just simply put this at very top of the page to resolve the header sent problem. <?php ob_start(); ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 6, 2007 Author Share Posted May 6, 2007 Hi yzerman, Thanks for the suggestions. I tried both, nstead of unsetting $_POST['submit'] before this line: echo "Thank you for voting!"; } add $submitted = $_POST['submit']; if ($submitted == $_POST['submit']) { //do the query and echo the results $submitted = NULL; } //close this if statement then change if (!isset($_POST['submit'])) { to if ($submitted != $_POST['submit']) { and also using the javascript. I am still having the same issue. I hit reload, it counts the vote over and over. Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 6, 2007 Author Share Posted May 6, 2007 Hi mmarif4u I tried the ob_start, still got the header errors. Thank you for the suggestion. Quote Link to comment Share on other sites More sharing options...
yzerman Posted May 6, 2007 Share Posted May 6, 2007 because when you hit refresh, you (the client) are resending the form. The only way around this is to redirect to another page. Now because this is a voting system, I imagine you are inputting a vote only for registered users. What you can do in that case, is add a field to your table if it doesn't exist and store the username. Then check for a vote on that particular subject from that username. If you are not requiring registered users, store the ip of the user with the vote - then before it is submitted, check the table for that IP address submitting a vote, and stop the query if that IP has already submitted a vote for that particular subject. Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 6, 2007 Author Share Posted May 6, 2007 Hi yzerman, Yeah I had already started that and added a column to my MySQL table for the user id. I think I may go that route also, cause that means they can't come back another day and revote. I thank all three of you for helping me out. I'll try the user one time vote thing. Thanks for your time. SC Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted May 6, 2007 Share Posted May 6, 2007 I am agree with yzerman suggestion. This is right here to avoid all these non stuff. Quote Link to comment Share on other sites More sharing options...
homer.favenir Posted July 30, 2009 Share Posted July 30, 2009 <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> you're missing echo <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.