Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Preventing Duplicate Form Submissions 13 Jul 2015 16:44 #13334

  • rnevins
  • rnevins's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 24
  • Thank you received: 6
I have been having some issues with duplicate database records being created because sometimes my users will be impatient and click the "Save & Close" multiple times before the processing of the first click has completed. This is easy to duplicate. I did some research and found there are several techniques employed to prevent duplicates, such as:
1. After click disable the Button via Javascript
2. After click disable the Button via jQuery
3. Use form tokens to skip the processing of a submission if the form was already processed.

After experimenting a bit, I have implemented in test mode the 3rd technique, following the approach found here: spotlesswebdesign.com/blog.php?id=11

In my case, I put the following in the view.html.php displayXXX function: (no need to do the session_start()
if (!isset($_SESSION)) {
$_SESSION = array();
}
$_SESSION[] = uniqid('', true);
Then I put this in the form tmplate:
<input type="hidden" name="page_instance_id" value="<?php echo end($_SESSION); ?>" />

And finally, in the controller save() function, I modified the first part of it as follows:

CkJSession::checkToken() or CkJSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));


//Check the ACLs
$model = $this->getModel();
$item = $model->getItem();

$app = JFactory::getApplication();
$data = $app->input->post->get('jform', array(), 'array');
//rbn - prevent duplicate form submissions
$page_id_index = array_search($_POST, $_SESSION);
if ($page_id_index !== false) {

unset($_SESSION[$page_id_index]);



$result = false;
if ($model->canEdit($item, true))
{
$result = parent::save();
//Get the model through postSaveHook()
if ($this->model)
{
$model = $this->model;
$item = $model->getItem();
}
}
else
JError::raiseWarning( 403, JText::sprintf('ACL_UNAUTORIZED_TASK', JText::_('VOLUNTEER_JTOOLBAR_SAVE')) );

$this->_result = $result;

}


This seems to work but I would appreciate input on the following:

1. Is there any easier/simpler approach (must modify/fork three files for this solution).
2. Do you see where this could cause any unintended problems or interactions elsewhere?

Thanks

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.047 seconds
  I still don't believe he can really be human to do all this ! From all of the forums that I've ever participated in this is certainly the one that most encapsulates the feeling of being truly open source where everyone's opinions and contributions can and will shape the development of the service! It's truly awesome! Hope you enjoy cooking and look forward to reading and contributing to any of the editorial work that you proposed too!! Thanks
Gez (audibleid - JED)

Get Started