Js learning path: javascript prevents repeated form submission

Source: Internet
Author: User

First, use the flag. The following Code sets the checkSubmitFlg flag:

<Script language = "" javascript "">

Var checkSubmitFlg = false;

Function checkSubmit (){

If (checkSubmitFlg = true) {return false; // After the form has been submitted once, checkSubmitFlg changes to true, and cannot be submitted based on judgment.

}

CheckSubmitFlg = true;

Return true;

}

</Script>

<Form name = "form1" method = "post" onsubmit = "return checkSubmit ();">

........... </Form>

Second, set in the onsubmit event to invalidate the submit button after the first submission. 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 source code of the program conflicts with the WIN2000 registry, invalid pages will appear after the post is sent, so many non-malicious duplicate posts in the Forum will come up with a solution, avoid repeated submission with JS. The following are some source code:
<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 only a time value, that is, repeated submission is prohibited within 5 minutes. The value is set as happy as you are.
{
Document. formsubmitf. mypretime. value = mysec;
}
Else
{
Alert ('one click is enough. Please do not submit it again! Please be patient! 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 small. An initial time value is passed with an implicit variable.

// Here is the content you want to submit

<Input type = "button" value = "written" name = "button1" class = "4 round" onclick = 'formsubmit () '> & nbsp; <font class = "red"> (please wait !) </Font> & nbsp; <input type = "reset" value = "re-write" name = "button2" class = "4 round">
</Form>

With this code, repeated posts on the Forum are obviously reduced, but there is a drawback: refresh once and detection does not work. The advantage is that JS detection does not require additional permission support, as for how it works, you can use it without having to use it. (It is best to add detection to both the frontend and backend)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.