Click here to Skip to main content
15,943,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue where a form appears to be posting back twice. Once when I click the submit button, then once again after the "ok" button is clicked in an alert from the page. I am thinking it has to do with there being a submit button as well as an onsubmit set in the form tag.

The onsubmit in the form tag is calling a function called checkform() which checks the input fields for data and puts a mark next to it if not. Sort of a rudimentary validation.

Here is the basic code I am using. If anyone can suggest a better way or a solution, I would appreciate it. Thank you.

 <script language="JavaScript" type="text/javascript">
                  function checkform(form) {
                      var isformvalid = true;
                      // ** START **
                      if (document.forms["EnrollMyInventory"].elements["Dealer Name"].value == "") {
                          document.getElementById('dnError').innerHTML = "*"
                          isformvalid = false;
                      else {
                          document.getElementById('dnError').innerHTML = ""
                      if (document.forms["EnrollMyInventory"].elements["Dealer Number1"].value == "") {
                          document.getElementById('dNumError').innerHTML = "*"
                          isformvalid = false;
                      else {
                          document.getElementById('dNumError').innerHTML = ""
return isformvalid;

<form method="post" action="enrollmyinventorycopy2.asp" name="EnrollMyInventory" onsubmit="return checkform(this);" >

<span class="style3">Dealer Name: </span>
                  <span class="style3">
                  <input type="text" name="Dealer Name" value="<%=Request.Form("Dealer Name")%>"
                                    size="33"><label style="color:Red" id="dnError"></label>
                  Dealer Number:
                  <input type="text" name="Dealer Number1" value="<%=Request.Form("Dealer Number1")%>"
                                    size="33"><label style="color:Red" id="dNumError"></label>

<input type="submit" name="Submit" value="Submit" />
Updated 23-Jul-13 8:53am
ZurdoDev 23-Jul-13 22:06pm    
Where is your OK button?
Richard C Bishop 24-Jul-13 15:32pm    
I am using a "submit" type input as my OK button essentially.
ZurdoDev 24-Jul-13 16:14pm    
I know but you said that when you click the submit button it works but then posts back again when you click the OK button but I don't see an OK button. If you OK button is of type submit then it will postback.
Richard C Bishop 24-Jul-13 17:05pm    
Ok, I get what you are saying. I did not explain that clear enough. The "OK" button I was referring to was inside the "alert" window from the javascript. I ended figuring out my issue. I will post the solution.
ZurdoDev 24-Jul-13 17:07pm    
Yes, I had understood that but I didn't see where you had posted that code. Anyway, good to hear you have it resolved.

1 solution

I ended up figuring out the solution.

Instead of using the "onsubmit" inside the form tag and an "submit" type input, I used a regular button and called the checkform() function with the onclick. Inside the checkform() function, I called .submit() to submit the form after the validation occurs.
Share this answer
Member 14484240 7-Jun-19 2:58am    
Please suggest me.How to solve this issue.How to keep form data after submitting in Email ID.

<title>Registration Form

User registration




Educational Qualification:

Present Course(for student only):




Mode of Payment:



Payment Reference No:

Payment Reference Date:


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900