Sunday, 15 September 2013

Prevent double submission of form using jQuery

Prevent double submission of form using jQuery

Problem:
I have a script where the user can press the keys C or M to choose an
option. Once either key is pressed the form will be submitted. Since there
is a strict rule that each option can be submitted once, some press
multiple times.
Question:
How can I prevent users from submitting the form again after they have
pressed the C or M key? I want them to be able to press either key once
the page refresh after submission.
Code in PHP / jQuery:
if (in_array($checkstring, $footer))
{
echo '
<script type="text/javascript">
$(document).ready(function()
{
$(window).keypress(function(e)
{
if (e.which == 99)
{
$("input#left").val( 1 );
$("form").submit();
}
else if (e.which == 109)
{
$("input#right").val( 1 );
$("form").submit();
}
});
'.$warning.'
});
</script>
';
}

No comments:

Post a Comment