auto_submit_form

Auto Submit Form Using JavaScript

Hello Friends,

In this blog post, I am going to let you know about how to make an auto-submit form using javascript?

Usually, when you fill up any form you get an option ‘submit’ for submitting your form.

But Here we are going to explain you to develop an auto submit form.


What is this auto submit form and what is the benefit of this form?

A form without an option of auto submit is known as auto submit form. Here the timer is set in the form.

If the timer time is 20 sec then the form will automatically submit after 20 sec.

The form will not be submitted until you fill all the field correctly.

If you fail to submit the information under time or filled wrong information then a timer will again start from the beginning.

If you fill any field wrong then a popup will appear to refill the field with correct data and then finally form will be submitted.

You don’t have any worry for the form timer as a form will not be submitted until you fill it correctly.

You can use this functionality where you have a sensitive form necessarily to be filled.

this is also very useful in the case of online question paper forms where students don’t worry about the final

submission of the test paper as having very limited time to submit their form.

You can see the form sample in the below image.

autosubmit1


Software Used:

Install NetBeans IDE.

Install Xampp Server.


What is the procedure to develop this functionality?

This is a very simple procedure to develop the form, you just need to follow the below step by step procedure for the same.

Start your Apache server using Xampp control panel.

Open your NetBeans IDE and create a project say ‘Formautosubmit’ though you can have any name as per your wish.

Create few files as shown in the below image under this project.

autosubmit2


Now copy and paste the below code one by one to the respective files given below.


formautosubmit.html:

<html>
<head>
<title>AutoSubmit Form Example Using JavaScript</title>
<!-- Include CSS File Here-->
<link href="formautosubmit.css" rel="stylesheet" type="text/css"/>
<!-- Include JS File Here-->
<script src="formautosubmit.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<div class="main">
<form action="success.html" method="post" id="form">
<h2>AutoSubmit Form Example Using JavaScript</h2>
<span>Form will automatically submit in <b id="timer">20</b> <b>seconds</b>.</span>
<label>Name :</label>
<input type="text" name="name" id="name" placeholder="Name" />
<label>Email :</label>
<input type="text" name="email" id="email" placeholder="Valid Email" />
<label>Gender :</label>
<input type="radio" name="gender" value="Male" id="male" />
<label>Male</label>
<input type="radio" name="gender" value="Female" id="female" />
<label>Female</label></br></br>
<label>Contact No. :</label>
<input type="text" name="contact" id="contact" placeholder="Contact No." />
</form>
</div>
</div>
</body>
</html>

formautosubmit.js:

window.onload = function() {
// Onload event of Javascript
// Initializing timer variable
var x = 20;
var y = document.getElementById("timer");
// Display count down for 20s
setInterval(function() {
if (x <= 21 && x >= 1) {
x--;
y.innerHTML = '' + x + '';
if (x == 1) {
x = 21;
}
}
}, 1000);
// Form Submitting after 20s
var auto_refresh = setInterval(function() {
submitform();
}, 20000);
// Form submit function
function submitform() {
if (validate()) // Calling validate function
{
alert('Form is submitting.....');
document.getElementById("form").submit();
}
}
// To validate form fields before submission
function validate() {
// Storing Field Values in variables
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var contact = document.getElementById("contact").value;
// Regular Expression For Email
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
// Conditions
if (name != '' && email != '' && contact != '') {
if (email.match(emailReg)) {
if (document.getElementById("male").checked || document.getElementById("female").checked) {
if (contact.length == 10) {
return true;
} else {
alert("The Contact No. must be at least 10 digit long!");
return false;
}
} else {
alert("You must select gender.....!");
return false;
}
} else {
alert("Invalid Email Address...!!!");
return false;
}
} else {
alert("All fields are required.....!");
return false;
}
}
};

formautosubmit.css:

/* Below line is used for online Google font */
@import url('https://fonts.googleapis.com/css?family=Oswald');
h2{
background-color: #dadad4;
padding: 30px 35px;
margin: -10px -50px;
text-align:center;
border-radius: 10px 10px 0 0;
}
span{
display: block;
margin-top: 10px;
font-weight:bold;
}
b{
color:red;
}
.back{
text-decoration: none;
border: 1px solid rgb(0, 143, 255);
background-color: rgb(0, 214, 255);
padding: 3px 20px;
border-radius: 2px;
color: black;
}
center{
font-size: 31px;
}
hr{
margin: 10px -50px;
border: 0;
border-top: 1px solid #ccc;
margin-bottom: 25px;
}
div.container{
width: 900px;
height: 610px;
margin:35px auto;
font-family: 'Oswald', sans-serif;
}
div.main{
width: 306px;
padding: 10px 50px 0px;
border: 2px solid gray;
border-radius: 10px;
font-family: raleway;
float:left;
margin-top: 30px;
background-color: #eae0e0;
margin-left: 240px;
}
input[type=text]{
width: 100%;
height: 40px;
padding: 5px;
margin-bottom: 25px;
margin-top: 5px;
border: 2px solid #ccc;
color: #4f4f4f;
font-size: 16px;
border-radius: 5px;
}
input[type=radio]{
margin: 10px 10px 0 10px;
}
label{
color: #464646;
text-shadow: 0 1px 0 #fff;
font-size: 14px;
font-weight: bold;
}
input[type=submit]{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 100%;
border-radius: 5px;
padding: 10px 0;
outline:none;
}
input[type=submit]:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}

success.html:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Form Submitted</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><h1 style="text-align: center;">Your Form has been submitted successfully</h1></div>
<a href="formautosubmit.html"><h3 style="text-align: center;">Back</h3></a>
</body>
</html>

Now you are ready to run and test your project. Run the HTML file to fill the auto submit form.


In a case of any queries, you can write us at anurag@a5theory.com we will get back to you ASAP.

Hope! you would have enjoyed this post.

Please feel free to give your important feedbacks in the comment section below.

Have a great time! Sayonara!