• Resolved admiralchip

    (@admiralchip)


    Hi,

    I’m building a custom plugin that involves submitting data using a form. The form works quite well but it is susceptible to double submission. How can I prevent double form submission?

    I used this. It disables the submit button but it prevents inserting of the values into the database table:

    <form ... onsubmit="myButton.disabled = true; return true;">

    How can I fix this?

    Thanks in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Dion

    (@diondesigns)

    On the page that contains the form, add this script:

    <script>
    var dd_submitted = false;
    
    function dd_if_submitted() {
    	if (dd_submitted) {
    		return false;
    	} else {
    		dd_submitted = true;
    		return true;
    	}
    }
    </script>

    And use the following in the form:

    <form ... onsubmit="return dd_if_submitted()">

    Please note that all solutions to stop double-posting will fail if the user has disabled javascript in their browser.

    Thread Starter admiralchip

    (@admiralchip)

    Thanks for your reply!

    I tried the code above but it didn’t work for me. Not sure why.

    Dion

    (@diondesigns)

    Hmm…that should have worked. Here’s another option to try. On your submit button, add something like this:

    <input type="submit" ... onclick="this.style.visibility='hidden'" />

    What this does is hide the submit button after it’s been clicked. That should stop double-posting as well.

    Thread Starter admiralchip

    (@admiralchip)

    Thanks! I’ll give it a try.

    Your original idea should be fine — if it’s changed to use readonly instead of disabled. The latter prevents the submission of the element’s value (i.e. if the form processing script is expecting the $_POST['save'] value or whichever, then it won’t work).

    <form … onsubmit=”myButton.readonly=true; return true;”>

    Thread Starter admiralchip

    (@admiralchip)

    Thanks everyone for your help. It works now. I used the second option given by @diondesigns . ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to prevent double form submission?’ is closed to new replies.