Forum OpenACS Development: Doubleclick Protection

Collapse
Posted by Malte Sussdorff on
Thanks to Dave's hint I enabled a general double click protection on submit buttons. The benefit over the existing way for double click protection is due to the fact that you do not have to rely on a key. So if you want to send an e-mail and hit the send button twice, the e-mail is send twice. You do not want this :).

Down below is a diff for scrutiny, what I did is:

- I appended an onclick event to the submit buttons which will store the name and value of the clicked button and then disable it. Last but not least the submit is executed.

- Added two elements in ad_form declaration:
-- __submit_button_name
-- __submit_button_value

These store the name and value of the clicked submit button. This is necessary as disabled elements are not submitted.

- changed template::form::formbutton to deal with the new situation.

- Set the clicked button with it's name in the ad_form environment so it can be used in a "if $button eq preview".

=====================

Index: form-procs.tcl
===================================================================
RCS file: /cvsroot/openacs-4/packages/acs-templating/tcl/form-procs.tcl,v
retrieving revision 1.32
diff -r1.32 form-procs.tcl
231c231,237
< }
---

} elseif {[string match "__submit_button_name" [ns_set key $form $i]] } {

# This is for the newly added __form_button element
# Which we need to use for double click protection

set button [string range [ns_set value $form $i] [string length "formbutton:"] end]
}
234a241,244
if {$formbutton eq "" && [exists_and_not_null button]} {
set formbutton $button
}

Index: widget-procs.tcl
===================================================================
RCS file: /cvsroot/openacs-4/packages/acs-templating/tcl/widget-procs.tcl,v
retrieving revision 1.40
diff -r1.40 widget-procs.tcl
328a329,333
# This is the double click protection
if {$type eq "submit"} {
append output "onclick=\"this.form.__submit_button_name.value=this.name; this.form.__submit_button_value.value=this.value; this.disabled=1; this.form.submit();\""
}

341a347
Index: form-processing-procs.tcl
===================================================================
RCS file: /cvsroot/openacs-4/packages/acs-tcl/tcl/form-processing-procs.tcl,v
retrieving revision 1.53
diff -r1.53 form-processing-procs.tcl
753a754,757
# add the hidden button element
template::element create $form_name "__submit_button_name" -datatype text -widget hidden -value ""
template::element create $form_name "__submit_button_value" -datatype text -widget hidden -value ""

1008a1013
1019a1025,1031
# Update the clicked button if it does not already exist
uplevel #$level {
if {![exists_and_not_null ${__submit_button_name}]} {
set ${__submit_button_name} ${__submit_button_value}
}
}

Collapse
2: Re: Doubleclick Protection (response to 1)
Posted by Malte Sussdorff on
After some more testing I found out that this will not work if you are trying to call the variable $button outside the ad_form block. Will try to find a workaround.