Jump to content

PHP:How to avoid multiple click on submit button


pascal_22

Recommended Posts

Hello,

 

I have a little problem, that i'm pretty sure, that can be solve easily but for now, i have no idea.

 

I have a register form, so when user click submit button i do some validation in JS and after a fully validation in PHP.

In my Javascript validation, at the beginning of this function i code:

function VerifForm()
{				
document.getElementById('btOk').style.visibility = "hidden";
document.getElementById('btReset').style.visibility = "hidden";
document.getElementById('boxLoader').style.visibility = '';
......

 

So if JS is enabled, the submit button and reset button are hidden and the loaderbox is displayed when a user click on the submit button.

All is working correctly.

 

But if user has javascript disabled, the loaderbox always appear because the code

document.getElementById('boxLoader').style.visibility = "hidden";

at the end of the page  is not executed due the JS disable and when user click on submit button, my button are not hidden, so, rarely, that some users click twice or three time on it, so it makes double,triple insert in my database...

 

It there a way to stop double/triple submit of a  form when JS is disabled? Or hide button....?

 

Thanks

Pascal

Link to comment
Share on other sites

use a session variable to keep track of whether or not the form has been submitted.  example code

 

your_form_handling_script.php

<?php
session_start();

if ( !isset($_SESSION['submitted']) ) {

  // your regular stuff here that checks if $_POST exists, validates form fields, inserts into db, etc...

  // if all of the above stuff checks out and form is successfully completed 
  // (make sure to only set this if everything is successfully done; you don't
  //  want to set this if visitor failed a validation or something, because then they won't be able to re-submit...
  $_SESSION['submitted'] = true;  

} else {
  // form was already submitted, tell visitor to stop being submit-button-trigger-happy or do nothing or whatever
}

 

only problem with this is if visitor clears their cookies it will destroy the session and they can resubmit.  Only real way around that is to basically put the form behind a login system and store a flag value in a database or flatfile with the username and instead of using a session variable flag, check the database/flatfile to see if user already submitted.  But EVEN THEN, wouldn't stop visitor from creating a new account!  But anyways, if you're simply trying to deter trigger-happy people then the above code should work just fine, because beyond that, people are past being trigger-happy and are purposely trying to resubmit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.