Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Submit button

Status
Not open for further replies.

vvlad

Programmer
May 27, 2002
151
DE
Hi,

I have to disable the Submit button after a form has been submitted, but I do no find the right way to do it.

Code:
<form action ="javascript:alert('Submited');" name = "test" onSubmit = "return testForm();">
	<input type = "text" name = "t"  />
	<br />
	<input type="submit" value="Submit1" name="B1" onclick="return confirm('Confirm this to submit the form.')">
	<br />
	<input type="submit" value="Submit2" name="B2" onclick="B2.disabled = true; return confirm('BlaBlaBla')">
	<br />
	<input type="submit" value="Submit4" name="B4" onclick="B4.disabled = true; if(confirm('BlaBlaBla')) document.forms[0].submit();">
</form>

<script language = "JavaScript">

function testForm()
{

	if(test.t.value != "")
	{
		alert("Testing - OK!");
		return true;
	}
	else
	{
		alert("Testing - ERROR!");
		return false;
	}
}

</script>

B1 works fine, but does not disable the button.
B2 disables the button, but does not submit the form.
B3 disables the button and submits the form, but also skips the validation routine (testForm()).

I guess I could solve the problem in the code of the validation routine or in the first line of code of the form, but the buttons are dinamically generated through a function that is called from several pages which all use different form names and validation routines names, so I would like to do it in the line with the buttons code.

I would also like to avoid the "visibility = hidden" property.

Any hints?


vlad
 

You should really do any testing for duplicate submission server-side. That way, people surfing without JS won't be able to mess up your system.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Dan

True, but this would slow down the application.

And the application runs on the Intranet of the company where everybody has IE with JavaScript enabled.

vlad
 

However, if that is not an issue for you, try this:

Code:
onclick="if(confirm('BlaBlaBla')) { this.disabled = true; this.form.submit(); ">

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 


Oops - missed off the closing brace:

Code:
onclick="if(confirm('BlaBlaBla')) { this.disabled = true; this.form.submit(); } ">

 

Incidentally, calling a form's submit method bypasses any onsubmit event handler you may have in place.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks for your input, Dan.

1. Your example also skips the validation.
2. I know that submit bypasses the onsubmit, I was just hopping someone has another idea.


vlad
 

Why not move your button disabling code into the testform function? If you only disable the button if the validation has worked, then you should be set.

Then remove the onclick handler altogether.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I agree with Dan. If you're going to disable the submit button after the form has been submitted, then it seems to me the logical place to do it is in the onsubmit handler.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks guys, I was afraid of that.

I wanted to avoid modifying the code in about 20 validation routines, but that seems to be the only way.

vlad
 

Dan
Just curious - how do you manage to answer in (almost) all threads?

vlad
 

How? I just type what I instinctively think (sometimes putting foot in mouth on the way)

Why? That is the real question... one for which I don't like the answer I gave myself, but am striving to resolve.

I know that probably doesn't clarify anything much, but as this is the JavaScript forum, not the philosophy forum, I'm sure you'll forgive me ;o)

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Dan: Don't worry, it's only temporary. After a while you get over it.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top