Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

UPDATED FOR CLARIFICATION

I've got this rating plugin that keeps submitting & duplicating the rating data on page refresh. When a user wants to add a rating, they have to click a button that takes them to "mysite.com/?review=true"

After they submit their review, the browser stays on "mysite.com/?review=true"

Upon refreshing the browser, the review is resubmitted, causing duplicate entries.

How can I check against duplicates in the database and stop this?

http://pastebin.com/c6wPGRD5 - class.php

http://pastebin.com/tz4PvWtS - form submit.php

http://pastebin.com/9ebCxpMZ - plugin.php

share|improve this question

2 Answers 2

To prevent multiple button clicks, you can use JavaScript to disable the button.

jQuery('#review_member_button').on('click', function(evt) {
    jQuery(this).attr('disabled', 'disabled');
});

On the WordPress end, you can set a unique key for the form every time it's generated and check to see if a form with that key has been resubmitted. I would recommend setting a transient, since they're temporary and easily cached with a plugin like Batcache.

When you build your form:

<?php
    $token_id = md5( uniqid( "", true ) );
?>

<form>
    ... Other form stuff
    <input type="hidden" name="token" value="<?php echo $token_id; ?>" />
</form>

Then, when you process your form:

<?php
$token_id = stripslashes( $_POST['token'] );

// If the transient exists, this is a duplicate entry. So don't do anything
if ( ! get_transient( 'token_' . $token_id ) ) {
    return;
}

// If the transient doesn't exist, set it so we don't process the form twice
set_transient( 'token_' . $token_id, 'dummy-content', 60 ); // Cache for 1 minute

// ... do your other processing
share|improve this answer
    
I'm trying to follow but this is not a plugin i've built. Can you please check my pastebin? I can upload the other two files if you need more to see. pastebin.com/6UW87Teb –  Aliyah Dec 7 '12 at 5:54
    
The issue with this is that you set it before you submit the first time, making the form impossible to ever submit. I'd set the transient, check for it, and then remove it. The second time when it doesn't find it, you've got a duplicate. –  Tyler Carter Dec 7 '12 at 6:27
    
My above comment however makes it impossible to submit forms after the time limit has expired. Another method is generate the ID, and then create the transient on the form submit. The first time it hits, it won't find it, so it processes and then creates a transient. Further submissions will find the transient and cause it to fail. –  Tyler Carter Dec 7 '12 at 6:28
    
@Chacha102 You're right. I was setting the transient in the wrong place. Tried to add my answer too quickly. Nice catch. –  EAMann Dec 7 '12 at 16:17
    
@Aliyah Is this a plugin you're using? Your theme? Where are you creating/processing the form in the first place? Please edit your question to include these details so we can tell you where to customize code. –  EAMann Dec 7 '12 at 16:18

submitting & duplicating the rating data on page refresh.

If it's only an issue for page refreshes redirect the user after form submission. This will prevent re-submission because the browsers current request wasn't the form submission. Quick example: in your form handler...

<?php
// your form handler someplace

// save the rating data.

wp_redirect($page_they_came_from, 303);
exit;

If you need to prevent users from voting multiple times, set a cookie at the tail end of your form handler (then redirect, see above).

<?php
// you form handler someplace

// save rating data

$voted = isset($_COOKIE['_wp_voted']) ? explode(',' $_COOKIE['_wp_voted']) : array();

if(!in_array($the_item_for_voting, $voted))
{
    // save votes here.

    $voted[] = $the_item_for_voting;
}

setcookie(
    '_wp_voted',
    implode(',', $voted),
    time() + (60 * 60 *24), // one day
    '/', // the whole site
    COOKIE_DOMAIN, // this is set by WP, or in your wp-config.php
    false,
    true
);

wp_redirect($page_they_came_from, 303);
exit;
share|improve this answer
    
Take a look at my pastebin and tell me how I should handle it. pastebin.com/6UW87Teb –  Aliyah Dec 7 '12 at 5:36
1  
I just told you how to handle it -- do all your form saving logic, then redirect the user to the page (URL) from whence the came. –  chrisguitarguy Dec 7 '12 at 5:39
    
I did not build this plugin, would appreciate if you could possibly take some code from my pastebin and tell me how to implement it properly. I added the wp_redirect to the end of the code and instead of taking me to the ratings page it redirected me without letting me vote first. Thanx. –  Aliyah Dec 7 '12 at 5:43
    
Ive edited my question. Hopefully you can assist me now. –  Aliyah Dec 7 '12 at 5:52
1  
The redirect status should be a 303. –  toscho Dec 7 '12 at 11:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.