Preventing second click

O

Oleg Konovalov

Hi,

I have a Java/JavaScript Web GUI application where I perform a lot of long
DB operations
[e.g. massive SQL Insert's],
which takes 5-60 secs to perform.
Sometimes user double-clicks the button or just gets impatient and clicks
again, which creates duplicate records.
So I am trying to disable the button as soon as it is clicked, and as soon
as it's done, re-enable it again.

I tried to do it in Javascript, just simple:
<input... name=Save... onclick="enabled=true;">
and as soon as form reloads, it re-enables the button automatically.
That works in some cases, however when I need to do some other Javascript
operation (e.g. validate the fields on the screen), disabling the button
automatically stops both Javascript and associated form action in Java
which is totally unacceptable.

Is there any other simple solution to such problems in Java or Javascript ?

Thank you in advance,
Oleg.
P.S.: It probably doesn't matter much, but that is a Cocoon2.0/XSLT app
[think of servlet app]
with Actions in Java, using JDK1.4.2 and IE6.
 
E

Erwin Moller

Oleg said:
Hi,

I have a Java/JavaScript Web GUI application where I perform a lot of long
DB operations
[e.g. massive SQL Insert's],
which takes 5-60 secs to perform.
Sometimes user double-clicks the button or just gets impatient and clicks
again, which creates duplicate records.
So I am trying to disable the button as soon as it is clicked, and as soon
as it's done, re-enable it again.

I tried to do it in Javascript, just simple:
<input... name=Save... onclick="enabled=true;">
and as soon as form reloads, it re-enables the button automatically.
That works in some cases, however when I need to do some other Javascript
operation (e.g. validate the fields on the screen), disabling the button
automatically stops both Javascript and associated form action in Java
which is totally unacceptable.

Is there any other simple solution to such problems in Java or Javascript
?

Thank you in advance,
Oleg.
P.S.: It probably doesn't matter much, but that is a Cocoon2.0/XSLT app
[think of servlet app]
with Actions in Java, using JDK1.4.2 and IE6.

Hi,

I am unsure how you setted up your application.
If somebody presses the button, a submitaction SHOULD happen, right?
So if that action takes 60 seconds before the server responds that it is
ready, what could users do in the meantime?

But maybe you setted it up quite differently.
Maybe this helps:
Instead of a input element that is submit, use type="button" with an onClick
handler
<form name="testform" action="longprocess.jsp" method="post">
<input type="button" value="Click me once" onClick="doPost();">
</form>

<script type="text/javascript">
var butClicked = false;
function doPost(){
if (!butClicked){
butClicked = true;
document.forms["testform"].submit();
} else {
alert("Be patient!");
}
}
</script>


But I am unsure in what state the browser is when the form is submitted, and
the server is working on the answer. If it leaves the current content, this
might help.

Good luck.

Regards,
Erwin Moller
 
A

ASM

Erwin Moller a écrit :
Oleg said:
I have a Java/JavaScript Web GUI application where I perform a lot of long
DB operations
[e.g. massive SQL Insert's],
which takes 5-60 secs to perform.
Sometimes user double-clicks the button or just gets impatient and clicks
again, which creates duplicate records.
So I am trying to disable the button as soon as it is clicked, and as soon
as it's done, re-enable it again.

as son as it's done what ?
But maybe you setted it up quite differently.
Maybe this helps:
Instead of a input element that is submit, use type="button" with an onClick
handler
<form name="testform" action="longprocess.jsp" method="post">

Why not a submit button ?

<input type="submit" value="Click me once"
onClick="this.disabled=true;">
 
E

Erwin Moller

ASM said:
Erwin Moller a écrit :
Oleg said:
I have a Java/JavaScript Web GUI application where I perform a lot of
long DB operations
[e.g. massive SQL Insert's],
which takes 5-60 secs to perform.
Sometimes user double-clicks the button or just gets impatient and
clicks again, which creates duplicate records.
So I am trying to disable the button as soon as it is clicked, and as
soon as it's done, re-enable it again.

as son as it's done what ?

Beats me too...

Maybe the page contains a(n) (I)frame, or an applet or something that
handles the posting and waits for the process to finish.

One cannot say based on the information given, but I expect the app is set
up in a strange way.

Why not a submit button ?

Because I wanted to annoy the impatient visitor with an alert. :p

Regards,
Erwin Moller
 
R

RobG

Erwin said:
Oleg said:
Hi,

I have a Java/JavaScript Web GUI application where I perform a lot of long
DB operations
[e.g. massive SQL Insert's],
which takes 5-60 secs to perform.
Sometimes user double-clicks the button or just gets impatient and clicks
again, which creates duplicate records.
So I am trying to disable the button as soon as it is clicked, and as soon
as it's done, re-enable it again.
[...]

I am unsure how you setted up your application.
If somebody presses the button, a submitaction SHOULD happen, right?
So if that action takes 60 seconds before the server responds that it is
ready, what could users do in the meantime?

But maybe you setted it up quite differently.
Maybe this helps:
Instead of a input element that is submit, use type="button" with an onClick
handler

It would be better to use a regular submit button and an onsubmit
handler.
<form name="testform" action="longprocess.jsp" method="post">

<input type="button" value="Click me once" onClick="doPost();">

</form>

<script type="text/javascript">
var butClicked = false;

function checkDouble() {
if (butClicked) return false;
butClicked = true;
}

If you are using AJAX, you'll need to reset the value of butClicked
when the response comes back if you want to submit the form again. It
would be tidier to wrap the whole thing in an object to get rid of the
separate global variable.
 
A

ASM

Erwin Moller a écrit :
Because I wanted to annoy the impatient visitor with an alert. :p

and submit button lost his voice ? :p

onClick="this.disabled=true;
alert('MySql at work !\nplease wait ...\n1 or 2 minutes');">
 
E

Erwin Moller

ASM said:
Erwin Moller a écrit :

and submit button lost his voice ? :p


onClick="this.disabled=true;
alert('MySql at work !\nplease wait ...\n1 or 2 minutes');">

Yes, that works fine too of course.
But you have to admit I used a whole lot more code to produce the same
result.
;-)

Erwin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top