Discussion on the Issues of Image Button Submission and Form Repeated Submission

Discussion on the Issues of Image Button Submission and Form Repeated Submission
In many cases, in order to beautify the form, the submit button is replaced with an image. However, if you don't pay attention to the details, it will cause repeated submission of the form, leading to some inexplicable errors, such as repeated insertion of database records.
Let's look at the following simple form code:

Copy code
The code is as follows:

<form id="loginform" name="loginform" action="upload/2022/web/<input type=image src=login.gif" name="imagesubmit" />
</form>

This code is correct and there will be no duplicate submission problem. “<input type="image">” actually has the same function as “<input type="SUBMIT">”. Clicking on the image will execute the submit() operation.
But some people are not assured, and add an onclick action to the image, such as the code:

Copy code
The code is as follows:

<form id="loginform" name="loginform" action="upload/2022/web/<input onclick=document.loginform.submit() type=image src=login.gif" name="imagesubmit" />
</form>

Now clicking the image button once will submit it twice, resulting in duplicate submission. Its function is equivalent to:

Copy code
The code is as follows:

<input type="SUBMIT" onclick="submit()">

Of course that is not right.
If you must use the onclick event, you can use <img> instead of <input type="image">. The following code can also work normally:

Copy code
The code is as follows:

<form id="loginform" name="loginform" action="upload/2022/web/<img onclick=document.loginform.submit() src=login.gif" name="imagesubmit" />
</form>

I encountered this problem myself, which caused the graphic verification code submitted by the user when logging in to be incorrect. Think about it, the user has submitted twice, will the verification code be correct when submitting the second time?
2. How to prevent duplicate submission of image buttons?

Copy code
The code is as follows:

<input type="image" src="bt.gif" onclick="submit()">

How to prevent duplicate submission of such a button?
Solution:

Copy code
The code is as follows:

<input type="image" src="bt.gif" onclick="submit();this.disabled=true">

This method can avoid repeated submission using image buttons, but now there are three such buttons together. I want to press one of them, and all three can no longer be submitted.
Solution:

Copy code
The code is as follows:

<script language="JavaScript">
function test(){
for(i=0;i<document.getElementsByName('t1').length;i++)
document.getElementsByName('t1')[i].disabled=true;
}
</script>
<form name="f1" method="post" action="1.htm" target="_blank" onsubmit="test()">
<input type="image" name="t1" src="upload/2022/web/newtopic.gif">
<input type="image" name="t1" src="upload/2022/web/newtopic.gif">
<input type="image" name="t1" src="upload/2022/web/newtopic.gif">
</form>

There are two ways to submit a form using an image:
1.<input type="image" src="xxx.gif" >
This image will automatically submit the Form, that is, type="submit". If you need to make a judgment or test before submission, use

Copy code
The code is as follows:

<input type="image" src="xxx.gif" onclick="return dosubmit();">

However, submitting the form in this way will cause the form to be submitted twice, which often results in form elements being submitted repeatedly and the database being written abnormally! !
The problem is especially serious when using IE, but there will be no error when using Firefox! At this time, please note that you must set the database to be unique for the same information!
Reason: The description of image in HTML is "Create an image control that will cause the form to be submitted immediately when clicked."
2.<img alt="Submit" src="xxx.gif" onclick="return dosubmit();" style="cursor:pointer;">
This method of submission is normal and has no problem, the effect is the same as above. Therefore, please use the first method less often to submit data, especially in struts applications. Note: css: cursor: hand can only be recognized by IE, not Firefox. But pointer is compatible!
Note! No matter which method you submit, it must be contained between <form></form>, otherwise, the submission will be invalid.

<<:  MySQL learning database operation DML detailed explanation for beginners

>>:  Practice of using Tinymce rich text to customize toolbar buttons in Vue

Recommend

Tudou.com front-end overview

1. Division of labor and process <br />At T...

Vue implements form validation function

This article mainly describes how to implement fo...

Detailed explanation of Vue router routing guard

Table of contents 1. Global beforeEach 1. Global ...

Let's talk about bitwise operations in React source code in detail

Table of contents Preface Several common bit oper...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

MySQL Database Basics: A Summary of Basic Commands

Table of contents 1. Use help information 2. Crea...

Detailed explanation of JS memory space

Table of contents Overview 1. Stack and Heap 2. V...

IE6 implements min-width

First of all, we know that this effect should be ...

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...

Vue front-end development auxiliary function state management detailed example

Table of contents mapState mapGetters mapMutation...

What are the file attributes of crw, brw, lrw, etc. in Linux?

What is a file? All files are actually a string o...

4 ways to view processes in LINUX (summary)

A process is a program code that runs in the CPU ...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

React realizes secondary linkage (left and right linkage)

This article shares the specific code of React to...

Pay attention to the use of HTML tags in web page creation

HTML has attempted to move away from presentation...