0

I have a button that acts as a submit:

<button  id='bet_horse' name='bet_horse' type="submit" class='btn btn-default bet_btn' >
     Submit This Info    
</button>

And a way for the post to be handled:

if ($_POST) {
$countInserts = 0;
if (isset($_POST['bet_horse'])) {
    if ($countInserts < 1) {
        $bs->insertbet_betslip(
            'horse',
            $current_user2->ID, 
            mysql_real_escape_string($_POST['nameMarket']),
            mysql_real_escape_string($_POST['nameType']), 
            $_POST['p_name'], 0, 
            $_POST['odds'], 
            $_POST['bettilltime'], 
            $_POST['bettilldate'], 
            date("H:i:s", time()),
            date("Y-m-d"),
            is_user_logged_in() ? true : false
        );
    }
//header("Location: /"); //This cannot be done becaause headers are already sent.
}

But after this I need to be able to stop the post from posting, the only ways I found of doing this is shown in the comment in the code, however I could not do this because of a lot of background WordPress processes, another way I thought to do it was with the if ($countInserts < 1 ) { }, but again didn't work, I have seacrhed for an answer but have been unsucessful, can anybody help?

Martin
  • 22,212
  • 11
  • 70
  • 132
  • 1
    You could use exit() to stop processing – Frank Apr 22 '15 at 12:25
  • 1
    Would that not mean the form would have to be resent then to keep the page active and live? –  Apr 22 '15 at 12:25
  • 1
    What does that mean exactly, *"stop the post from posting"*?! – deceze Apr 22 '15 at 12:27
  • 1
    @deceze, it means that when the page is refreshed, it asks you to "confirm form resubmission" and re-sends the post back to the server, what I want to happen is for the post NOT to go back to the server and be stopped where it is. –  Apr 22 '15 at 12:28
  • 1
    @swiftie821 That is a function of the browser, it's not something you can change. – Styphon Apr 22 '15 at 12:29
  • 1
    @Styphon, if you look at a site like Facebook, you permernantly send information to the server, I am usure of this is post or not, but when you refesh the page you are not asked to resubmit the form, so what would be the best "work around" for avoiding this as I only want one of the information desired sent back? –  Apr 22 '15 at 12:30
  • 1
    @swiftie821 Use Ajax to submit the form. – Styphon Apr 22 '15 at 12:31
  • 1
    @Styphon, this would be done on a button click, not a submit, is that correct? –  Apr 22 '15 at 12:32
  • 1
    Duplicate of http://stackoverflow.com/questions/5294378/how-could-prevent-refresh-page-problem-while-submitting-form/5294386#5294386 – deceze Apr 22 '15 at 12:32
  • 1
    You can do it on either. If you use jQuery you can intercept either the button click or the form submit. I'd recommend intercepting the form submit. – Styphon Apr 22 '15 at 12:32
  • @Styphon, may I ask why you would intercept the submit over a button click? (Pure intrigue on another programmers opinion) –  Apr 22 '15 at 12:33
  • 1
    Because clicking a button is not the only way to submit a form. You can do it with the enter button as well. If you intercept the button click rather than the form submit you lose that functionality. – Styphon Apr 22 '15 at 12:35
  • 1
    @Styphon, Thank you for that, I will do my best to get that in place, thank you again. –  Apr 22 '15 at 12:37
  • Just to make that clear: I'm pretty sure you're looking for a **POST/REDIRECT/GET pattern**. See aforelinked duplicate. This is a super common technique, no need to invent anything via Javascript. – deceze Apr 22 '15 at 12:38
  • @deceze no, that's not what the OP is after. For example, you submit a search form to a page. Then you refresh the page, the browser asks you to resubmit the data again. That's what the OP wants to stop. – Styphon Apr 22 '15 at 12:44
  • @Styphon Still: POST/REDIRECT/GET. A search form shouldn't be submitted via POST to begin with, or at least be converted into a GET request from there using P/R/G. – deceze Apr 22 '15 at 12:45
  • @deceze It's not POST/REDIRECT/GET, there is no redirect. The search form was an example (a bad one) just to illustrate what the user is after. If you wanted the posted data to be GET, the OP could just change the forms method. – Styphon Apr 22 '15 at 12:48
  • @Styphon There is not redirect, **but there should be one**. The point of P/R/G is precisely to prevent re-POSTing by refreshing. – deceze Apr 22 '15 at 12:49
  • @deceze right, sorry, got there in the end hehe. – Styphon Apr 22 '15 at 12:50

2 Answers2

0

https://codex.wordpress.org/Function_Reference/wp_redirect

wp_redirect( $location, $status );
exit;

For example:

wp_redirect( home_url() ); exit;

Or if Headers was submitted You can use JavaScript:

echo '<script>window.location = "/"</script>';
-1

Your question is a little unclear, but if you want to stop the POSTED data being resubmitted with a page reload, or page refresh then try reading this article:

http://www.phpro.org/tutorials/Preventing-Multiple-Submits.html

with thanks to Fred-ii- for the original link!

In summary, you need to set a token id value from the $_SESSION into the form, and then check that the token id value given in the $_POST data matches the SESSION data. Once the form is submitted the first time you can then delete the $_SESSION token id and only commit changes if the $_SESSION id == $_POST id.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • That will allow you to identify if it's been reposted, but it doesn't stop it from being reposted. – Styphon Apr 22 '15 at 12:31
  • If you read the article it does exactly that, it stops refreshing the destination page from resubmitting the data because once the data has been submitted the SESSION value is deleted, so that the posted form data can never then equal the session value (As it's unset) – Martin Apr 22 '15 at 12:33
  • Yes, that's exactly what I said. However it doesn't stop the browser from asking you to refresh the page and resubmit the data. – Styphon Apr 22 '15 at 12:34
  • the data is resubmitted to the page, but the data is not acted upon within the PHP code (as it's already been submitted), so the data is ignored. I think this is what the OP is looking for. – Martin Apr 22 '15 at 12:36
  • I don't think it is, from his comments the OP doesn't even want the data to be resubmitted to the page. – Styphon Apr 22 '15 at 12:37
  • yes....... my answers shows how to prevent the data being ***re***submitted to the page and being used. . – Martin Apr 22 '15 at 12:38
  • 1
    I want the information to be gone after the first submit, the form should not even know that it ever existed, @Styphon hit the nail on the head there –  Apr 22 '15 at 12:39
  • ok :) It may be helpful to me (and others) to edit and clarify what you actually want in the question, but glad it's solved :) – Martin Apr 22 '15 at 12:41