Home Web Front-end JS Tutorial Two ways to prevent repeated submission of forms in js_javascript skills

Two ways to prevent repeated submission of forms in js_javascript skills

May 16, 2016 pm 05:20 PM

First type: is marked with flag. The following code sets the checkSubmitFlg flag:
Copy code The code is as follows:

<script language=""javascript"">

var checkSubmitFlg = false;

function checkSubmit(){

if (checkSubmitFlg ==true){ return false; //When the form is submitted once, checkSubmitFlg will become true, and it will not be submitted based on judgment.

}

checkSubmitFlg ==true;

return true;

}

< /script >

< form name=”form1” method=”post” onsubmit=”return checkSubmit();”>

………..< /form>

The second type: is set in the onsubmit event to invalidate the submit button after the first submission. The code is as follows:
Copy code The code is as follows:

<form action=”about:blank” method=”post” onsubmit =”getElementById('submitInput').disabled=true;return true ;" target="_blank">
<input type="submit" id="submitInput"/>
</form>
</body>
</ html>
</script>

Because the program source code conflicts with the WIN2000 registry, an invalid page will appear after the post is sent. As a result, there are many non-malicious duplicate posts in the forum. Later I came up with a way to use JS to avoid repeated submissions. Here is part of the source code:
Copy the code The code is as follows:

<script Language='JavaScript'>
function formsubmit() {
Today = new Date();
var NowHour = Today.getHours();
var NowMinute = Today.getMinutes();
var NowSecond = Today.getSeconds();
var mysec = (NowHour*3600) (NowMinute*60) NowSecond;
if((mysec-document.formsubmitf. mypretime.value)>600)
//600 is just a time value, which means repeated submission is prohibited within 5 minutes. Set the value as you like
{
document.formsubmitf.mypretime.value=mysec;
}
else
{
alert(' Just click once, please do not submit again! Please wait! Thank you for your cooperation! ');
return false;
}
document.forms.formsubmitf.submit();
}
</script>

</HEAD>

<BODY BGCOLOR="#FFFFFF">
<form name=formsubmitf id ="the" method="post" action="XXX.asp">
<input type=hidden name='mypretime' value='0'> //This sentence cannot be missing, use implicit variables to pass an initial time value

//Here is the content you want to submit

<input type="button" value="written" name="button1" class="4round" onclick='formsubmit()'> <font class="red">(Please click once , wait patiently! )</font> <input type="reset" value="rewrite" name="button2" class="4round">
</form>

Using this code, the number of duplicate posts in the forum has been significantly reduced. However, there is a disadvantage. The detection will not work after refreshing once. The advantage is that it uses JS detection and does not require additional permission support. As for the effect, it depends on whether you use it or not. It’s up to you (it’s best to add detection to both the front-end and back-end)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles