Code to prevent multiple submit

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

hambacon
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 07, 2012 10:41 am

Code to prevent multiple submit

Postby hambacon » Wed Nov 07, 2012 11:33 am

Hello, I'm relatively new to php and trying to come up with a voting type system. I'm using php and SQL to store my data. Also, I am working with Dreamweaver record sets. What I'm trying to do is allow a registered user to click on a submit button to vote for an item. As a result, I want the script to increment the items rating by 1. The problem I'm having is that when testing, I can click the submit button multiple times before the page reloads and it adds the number of times clicked to the item rating. This is the code I came up with to accept votes and add it to the database. The $sinceCreated variable is just set to equal 2 for now.

Code: Select all

$updatedisplayrating = "UPDATE item SET displayrating=itemrating/$sinceCreated WHERE itemid='$_POST[hiddenitemid]'";

//  Defining button functions
if(isset($_POST['VoteButton'])){
$VoteQuery = "UPDATE item SET itemrating=itemrating+1 WHERE itemid='$_POST[hiddenitemid]'";
mysql_query($VoteQuery, $con);
mysql_query($updatedisplayrating, $con);
};


Here is what I use for the button. Note: $row_rs_ItemByNumber['itemid'] is used from Dreamweaver record set.

Code: Select all

echo "<form action=index.php method=post>";
echo "<input type=hidden name=hiddenitemid value=" . $row_rs_ItemByNumber['itemid'];
echo "<td>" . "<input type=submit name=VoteButton value=Vote " . "</br>" . " </td>";
echo "</form>";


I was thinking I could use a second variable to solve the multiple submission problem. Thinking that the code runs on every page load and not every action on the page (button press). Something like this:

Code: Select all

$i=0;
//  Defining button functions
if(isset($_POST['VoteButton'])){
$i=1;
};

if($i=1){
$VoteQuery = "UPDATE item SET itemrating=itemrating+1 WHERE itemid='$_POST[hiddenitemid]'";
mysql_query($VoteQuery, $con);
mysql_query($updatedisplayrating, $con);
};

What is the reason that the second variable is still allowing multiple submissions? Wouldn't it just be setting $i=1 every time it's clicked and then running the if statement containing $VoteQuery to run only once per page load?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Code to prevent multiple submit

Postby seandisanti » Wed Nov 07, 2012 2:22 pm

it's all about how you implement your rating system. personally, i'd have a ratings table, with the fields : id,user_id,product_id,and rating. Then you can have a stored procedure that updates or inserts based on the combination of user_id, and product_id. I'd have it so that when a user accessed a product page where they could rate, the current overall rating and their personal rating are stored in variables and displayed, and then if they make a change, or resubmit, you will know because isset($userRating) will have been populated by an existing value.

hambacon
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 07, 2012 10:41 am

Re: Code to prevent multiple submit

Postby hambacon » Wed Nov 07, 2012 4:45 pm

The rating system is implemented more like a ranking of multiple items in a category. Here is an example: at this test page

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Code to prevent multiple submit

Postby seandisanti » Thu Nov 08, 2012 9:06 am

The only thing that changes is that you don't need the rating field. When they load the page, query the database to see if they've already submitted, if yes, indicate which they've voted for. Then checking if the the variable that stores their existing vote isset() lets you know if you need to insert or update if they vote again.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron