Introduction
One source of duplicate comments for Movable Type is users hitting the “POST” button multiple times, either through a key bounce or because of slow response. One amelorative option is to arrange for the “POST” button to be disabled before the comment data is submitted. This not only avoids duplicates in most situations but is useful feedback to the user that, yes, they did in fact push the button.
Solution
This turns out to be a bit trickier than it seems, because some browsers won’t send data from disabled objects. This is a problem because the “POST” button contains critical information needed to process the comment request. It is not sufficient to simply move that to a hidden object, because that will cause problems for “PREVIEW”. My solution is to have a hidden object and engage in a bit of object renaming during the disable process. I have tested this on IE 6, IE 7, FireFox 1.5, FireFox 2, Opera 8 and Opera 9, and it seems to work.
This is the JavaScript enhanced “POST” button and hidden object. It replaces the existing “POST” button.
<input type="hidden" name="mode" value="mode" />
<input type="submit" name="post_comment" id="post_comment" value=" POST "
onclick="document.comments_form.post_comment.disabled=true;
document.comments_form.mode.name = 'post';
document.comments_form.onsubmit();
document.comments_form.submit();"
/>
The Javascript disables the button, renames the “mode” object to “post” to put that as data in the submit, executes any other JavaScript expected to run on submit, and finally submits the form.
Installation
You will need to modify every template that accepts user comments. This will generally be the individual archive template and the preview comment template. It may also be the comment error tempate and the comment popup template.
You will need to modify the name of the FORM tag for comment submission. By default it is named “form” and this can cause hard to diagnose name collision problems. The actual name doesn’t matter, it is just important that it is unique. Use all lower case letters and underscores, nothing else. As you can see above, I renamed mine “comments_form”. I recommend doing the same unless you have a specific reason to use something else. When changing the name, change both the name and id tag and give them the same value1.
Once you’ve renamed the form, replace the current “POST” button with the lines quoted above. Change any instances of “comments_form” with the name you used for the FORM tag if that is different.
Notes
- The
nameattribute for the “POST” button must not be “post” as it is in the default template. - The
idattribute value for the “POST” button should be the same as thenameattribute value. - The
idattribute value and thenameattribute value for theFORMtag should be identical. This must also match the name following “document” in the JavaScript. - You can set the
valueattribute of the “POST” button to whatever you like — it has no effect on submitting comments, it is used only as the displayed text in the button.
1 This is because some browsers like the name attribute and some like the id attribute. There’s no good reason to not do both except for the extra typing.
Trackback URL: http://blog.thought-mesh.net/solidwallofcode/mt_projects/mt_patch_auto_d.php/ping
From Ask Dave Taylor!: How do you get your Submit button to disable on click? on Friday, 08 June 2007 at 09:01
Eeek. This sounds mighty complicated, I think the other brother can do it.
hello, thanks for this information. is it possible to use this with javascript form validation ? i tried but when javascript show message for a field not correctly fill, the form is sent anyway. thanks in advance, kelljery
It’s definitely possibly, it’s a matter of how much work would be needed. Basically, you would want to change the “onclick” code listed here to call the validation and then not proceed if the validation failed. Alternatively, you could remove the “onclick” code and merge in to validation code, to be called after validating. In either case, you would need to modify the Javascript in a non-trivial way.
Works like a dream and so simple, thanks so much for sharing!