Link to home
Start Free TrialLog in
Avatar of galor
galor

asked on

Disable pressing on a submit button twice

I want to disable pressing on submit button after the form has been submited, so my command will not processed twice.
When i use my 'inLoad' variable, the second press lock the current page in browser and the next page not reached.

What can i do?
 
Avatar of daniel_c
daniel_c

Change your submit button into button, and execute javascript function to do that!
Here is a simple example:

<SCRIPT LANGUAGE=Javascript>
  function DoIt()
  {
     document.form1.buSubmit.disabled = true;
     document.form1.submit();
  }
</SCRIPT>
<FORM name=form1 Action="somepage.asp" method="post">
Name: <INPUT TYPE=text>
<BR>
<INPUT TYPE=button Name=buSubmit Value="Submit" OnClick="DoIt()">
</FORM>
Here is a simple example:

<SCRIPT LANGUAGE=Javascript>
  function DoIt()
  {
     document.form1.buSubmit.disabled = true;
     document.form1.submit();
  }
</SCRIPT>
<FORM name=form1 Action="somepage.asp" method="post">
Name: <INPUT TYPE=text>
<BR>
<INPUT TYPE=button Name=buSubmit Value="Submit" OnClick="DoIt()">
</FORM>
<html>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!--
     var isSubmitComplete = false;
     
     function submitForm(){
          if(!isSubmitComplete){
               isSubmitComplete = true;
               alert("Form submit");
          }else{
               alert("Form already submitted");
          }
     }

//End-->
</script>
</HEAD>
<body>
<form method="post" action="try.html" name="f1">
     <input type="text">
     <input type="button" onClick="submitForm()" value="submit">
</form>
</body>
</html>
note: Button can't be disabled in Netscape
You can use DHTML to remove the button:
<html>
<head>
<script language="JavaScript">

function submitForm() {
     removeMe.innerHTML='';

     document.frontDoor.submit();

}
</script>
</head>
<body >
<form name='frontDoor' action='submitme.html'>
<input type=text name='login_id'>
<br>
<input type=text name='login_password'>
<div id='removeMe'>
<input type=button onclick='submitForm()'>
</div>
</form>
</body>
</html>

ASKER CERTIFIED SOLUTION
Avatar of hidayath
hidayath

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hidayath, please refer to EE ethics.
Leave your suggestion as a comment, and let galor select the best solution for him.
does anyone else see the irony that danielc hit the submit button twice to post his answer.  EE should implement his code:)
What do you mean beg3a? I didn't mean to say that I gave the best solution, just try to obey EE ethics.
Please refer to: https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20110692

No hard feeling though!

^_^
I was just pointing out the humor in the fact that when you posted your code about how to disable a person from submitting a form twice, you accidentally posted your code twice.  Get it?  haha.

As far as your code I think it should be chosen as the correct answer.
Hehehe, actually it was not because of submitting a form twice, but probably pressed refresh button twice!? ;-)
when we are talking about jokes...
how about this:
<html>
<title> checkAndSubmit() </title>
<head>
<SCRIPT LANGUAGE=JavaScript>
<!--
var submitted = false;
function checkAndSubmit() {
 if (!submitted) {
   document.myForm.mySubmit.value = "submitted...";
   return submitted = true;
 }
 document.myForm.mySubmit.value = "don't do this again.";
 return false;
}
// -->
</SCRIPT>
</head>
<body>
<FORM onSubmit="return checkAndSubmit()" METHOD=post name=myForm>
<input type=text name=Name>
<input type=submit name=mySubmit>
</form>
</body>
</html>

(perhaps jokes also earn points ;-))
Regards,
zvonko
upps...
when testing the joke on IE give it an action; like this:
<FORM onSubmit="return checkAndSubmit()" METHOD=post name=myForm action="http://localhost/test">

Otherwise it is to fast reloaded and you see no effects...
Avatar of galor

ASKER

I forgot to write that i use <A> tag and not button, so i can not use the 'disabled' property.
I use the removeMe.innerHTML="" answer.
Galor, you have given grade to the wrong person then!
It should be for dfc_106!

:)