Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to prevent duplicate submission in a form using jQuery?
Next article icon

How to prevent duplicate submission in a form using jQuery?

Last Updated : 28 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Preventing duplicate submission in a form using jQuery involves implementing measures to ensure that the form is only submitted once, regardless of how many times the user interacts with the submit button. To prevent duplicate form submissions in web applications using jQuery we can use various methods like One-time event binding and disabling submit button.

Use the below approaches to prevent duplicate submissions in a form using jQuery:

Table of Content

  • Using One-Time Event Binding
  • By disabling the submit button

Preventing duplicate submissions in a form using One-Time Event Binding

In this approach, jQuery's .one() method ensures that the form submission event is bound only once, preventing duplicate submissions. Upon submission, it disables the form elements and displays a success message. Further form submissions are blocked after the initial submission, ensuring data integrity.

Example: Implementation of preventing duplicate submission in a form with jQuery using jQuery One-Time Event Binding.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                    initial-scale=1.0">     <title>Prevent Duplicate Form Submission</title>     <style>         .navbar {             background-color: #333;             padding: 10px 0;             width: 100%;             top: 0;             z-index: 1000;         }          .logo-container {             display: flex;             justify-content: center;             align-items: center;         }          .logo {             width: 80px;             height: auto;         }          body {             margin: 0;             padding: 0;             display: flex;             flex-direction: column;             align-items: center;             min-height: 100vh;         }          #content {             display: flex;             flex-direction: column;             align-items: center;         }          h1 {             margin-top: 20px;             text-align: center;         }          form {             width: 300px;             margin-top: 20px;             padding: 20px;             border: 1px solid #ccc;             border-radius: 5px;             box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);         }          input[type="text"],         input[type="password"],         button {             width: 100%;             margin-bottom: 10px;             padding: 10px;             border: 1px solid #ccc;             border-radius: 5px;             box-sizing: border-box;         }          button {             background-color: #007bff;             color: #fff;             cursor: pointer;         }          button:disabled {             opacity: 0.6;             cursor: not-allowed;         }          button:hover:enabled {             background-color: #0056b3;         }          #successMessage {             display: none;             color: green;             margin-top: 10px;         }          #submittedData {             display: none;             margin-top: 20px;             padding: 10px;             background-color: #f5f5f5;             border: 1px solid #ccc;             border-radius: 5px;         }     </style> </head>  <body>     <nav class="navbar">         <div class="logo-container">             <img class="logo" src= "https://media.geeksforgeeks.org/gfg-gg-logo.svg" alt="Logo"/>         </div>     </nav>      <div id="content">         <h1>Using jQuery One-Time Event Binding to prevent               duplicate form submissions           </h1>          <form id="myForm">             <input type="text" name="username"                     placeholder="Username" required>             <input type="password" name="password"                     placeholder="Password" required>             <button type="submit">Submit</button>         </form>          <div id="successMessage">           Form submitted successfully!           </div>          <div id="submittedData"></div>     </div>      <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">       </script>     <script>         $(document).ready(function () {             $('#myForm').one('submit', function (event) {                 event.preventDefault();                  // Display success message                 $('#successMessage').show();                  // Show submitted data                 let formData = $(this).serialize();                 $('#submittedData').html(                   '<strong>Submitted Data:</strong><br>'                        + formData).show();                  // Disabling form fields and button                 $(this).find('input, button')                          .prop('disabled', true);             });         });     </script>  </body>  </html> 

Output:

nhl
Output

Preventing duplicate form submissions by disabling the submit button

This approach involves using jQuery, to disable the submit button immediately upon form submission. By disabling the button, users are unable to click it multiple times, effectively preventing duplicate form submissions, enhancing user experience and preventing unintended actions.

Example: Implementation of preventing duplicate submission in a form by disabling the submit button.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                    initial-scale=1.0">     <title>Prevent Duplicate Form Submission</title>     <style>         .navbar {             background-color: #333;             padding: 10px 0;         }          .logo-container {             display: flex;             justify-content: center;             align-items: center;         }          .logo {             width: 80px;             height: auto;         }     </style>     <script src="https://code.jquery.com/jquery-3.6.0.min.js">       </script>     <script>         $(document).ready(function () {             $('#myForm').submit(function (event) {                                    // Prevent default form submission                 event.preventDefault();                  // Check if the form has already been submitted                 if ($(this).data('submitted')) {                     return;                 }                  // Disable the submit button                 $('#submitBtn').prop('disabled', true);                  let formData = $(this).serialize();                  $('#submittedData').html(formData);                  $('#statusMessage').text('Submitting...');                  $(this).data('submitted', true);                  setTimeout(function () {                     $('#statusMessage').text('');                 }, 3000);             });         });     </script> </head>  <body>     <nav class="navbar">         <div class="logo-container">             <img alt="Logo" class="logo" src= "https://media.geeksforgeeks.org/gfg-gg-logo.svg" />         </div>     </nav><br />     <h2>Prevent duplicate submission           in a form using jQuery       </h2>      <form id="myForm">          <input type="text" name="name"                 placeholder="Name" required><br>         <input type="email" name="email"                 placeholder="Email" required><br>         <input type="submit" id="submitBtn"                 value="Submit">     </form>      <p id="statusMessage"></p>      <div id="submittedData">     </div>  </body>  </html> 

Output:

ff


Next Article
How to prevent duplicate submission in a form using jQuery?

A

ashishrettgg
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • jQuery

Similar Reads

    How to submit a form using ajax in jQuery ?
    Submitting a form using AJAX in jQuery allows sending form data to a server asynchronously without reloading the page. This method improves user experience by sending the data in the background and receiving a response without interrupting the user's interaction with the webpage.Syntax:$.ajax({type:
    2 min read
    How to stop a form submit action using jQuery ?
    In this article, we will learn how to stop a form submit action using jQuery. By default, the HTML form submits automatically. Submitting automatically leads to reloading the whole page again and again. Therefore for performing any operation, we have to prevent its default submission. Given a form,
    3 min read
    How to submit a form on Enter button using jQuery ?
    Given an HTML form and the task is to submit the form after clicking the 'Enter' button using jQuery. To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. html <!DOCTYPE html>
    2 min read
    How to disable form submit on enter button using jQuery ?
    There are two methods to submit a form, Using the "enter" key: When the user press the "enter" key from the keyboard then the form submit. This method works only when one (or more) of the elements in the concerned form have focus. Using the "mouse click": The user clicks on the "submit" form button.
    2 min read
    How to clear form after submit in Javascript without using reset?
    Forms in JavaScript serve to gather specific user information, essential in various contexts like recruitment or inquiry submissions. While offline forms can be freely distributed, online forms must maintain a consistent structure for all users. After filling out a form, submission is crucial, but d
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences