473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Disable a form submission link during validation?

Assume there's a form with it's action attribute all set to post to a
URL, but without a submit control. Form submission is done via a link
and I want to prevent the classic "double submit". Ignoring the server
side of things, does anyone see any holes with the following script? It
seems to work, but I'd appreciate other eyes on it. Maybe a
try/catch/finally wrapper of some sort to be sure the link is
re-enabled in the face of an exception. I understand there are (many)
other ways to do this (e.g. temporarily "remove" the link), but I'm
mostly curious about the
this.onclick=falseFn/this.onclick=arguments.callee combo and any
potential gotchas. Thanks.

function falseFn() {
return false;
}

// this is the onclick handler for the link
function submitLinkOnclick() {
this.onclick = falseFn; // disable link to prevent double-submit

var isOkToSubmit = false;
var form = document.getElementById("form");

// logic to see if it's OK to submit form (set isOkToSubmit = true)

if (isOkToSubmit) {
form.submit();
} else {
alert("blah blah blah");
this.onclick = arguments.callee; // re-enable link
}

return false;
}

Jul 26 '06 #1
4 2693
ks********@gmail.com wrote:
Assume there's a form with it's action attribute all set to post to a
URL, but without a submit control. Form submission is done via a link
and I want to prevent the classic "double submit". Ignoring the server
side of things, does anyone see any holes with the following script? It
seems to work, but I'd appreciate other eyes on it. Maybe a
try/catch/finally wrapper of some sort to be sure the link is
re-enabled in the face of an exception. I understand there are (many)
other ways to do this (e.g. temporarily "remove" the link), but I'm
mostly curious about the
this.onclick=falseFn/this.onclick=arguments.callee combo and any
potential gotchas. Thanks.
Two: if the user has JavaScript disabled or not available, they can't
submit the form.

The second (and probably bigger one) is that script execution on the
client is unreliable. You may end up with the form being submitted
multiple times anyway, or the user's first attempt to submit may not
work or be cancelled and your script may block subsequent submissions
unreasonably.

function falseFn() {
return false;
}

// this is the onclick handler for the link
function submitLinkOnclick() {
this.onclick = falseFn; // disable link to prevent double-submit
In order for 'this' to refer to the element on which the onclick
handler has been placed, you must be attaching the function dynamically
using something like:

theLink.onclick = submitLinkOnclick;

var isOkToSubmit = false;
var form = document.getElementById("form");
I'm not a big fan of having an element with an ID the same as the tag
name, then also having a local variable with the same name - what
'form' refers to starts to get confusing. IE will add a 'form'
property to the global object too.

// logic to see if it's OK to submit form (set isOkToSubmit = true)

if (isOkToSubmit) {
Since you changed isOkToSubmit to false above, this will always return
false and the form will never submit.

form.submit();
} else {
alert("blah blah blah");
this.onclick = arguments.callee; // re-enable link
So you will always re-set the onclick to the current function. At what
point were you going to set it to falseFn?

}

return false;
}
The whole approach seems flawed. If you want to try something on the
client, put a submit button in the form and have a global variable (or
the value of some hidden form field) set to 'not submitted' (or false
or whatever). When the form is submitted, check the value to determine
whether to submit the form or not and change the value of the
variable/field to 'submitted' (or true or whatever). The following
example uses the form name to add a property to a global object so you
can keep track of multiple forms:

<script type="text/javascript">

var submittedForms = {};

function checkSubmit(formRef){
if (formRef.name in submittedForms){
alert('submitted');
return false;
}
submittedForms[formRef.name] = true; // Any value will do
}
</script>

<form name="formA" action=""
onsubmit="return checkSubmit(this);">
<!-- rest of form -->.
<input type="submit">
</form>
But it's not a very reliable method of stopping multiple submissions.
--
Rob

Jul 26 '06 #2
Rob,

Thanks for looking it over. See inline.

RobG wrote:
ks********@gmail.com wrote:
Assume there's a form with it's action attribute all set to post to a
URL, but without a submit control. Form submission is done via a link
and I want to prevent the classic "double submit". Ignoring the server
side of things, does anyone see any holes with the following script? It
seems to work, but I'd appreciate other eyes on it. Maybe a
try/catch/finally wrapper of some sort to be sure the link is
re-enabled in the face of an exception. I understand there are (many)
other ways to do this (e.g. temporarily "remove" the link), but I'm
mostly curious about the
this.onclick=falseFn/this.onclick=arguments.callee combo and any
potential gotchas. Thanks.

Two: if the user has JavaScript disabled or not available, they can't
submit the form.
Sure, understood.
The second (and probably bigger one) is that script execution on the
client is unreliable. You may end up with the form being submitted
multiple times anyway, or the user's first attempt to submit may not
work or be cancelled and your script may block subsequent submissions
unreasonably.
I'm not sure I follow. In what way is script execution unreliable? Do
you just mean that JavaScript could be disabled? Under what conditions
would the form be submitted multiple times?
function falseFn() {
return false;
}

// this is the onclick handler for the link
function submitLinkOnclick() {
this.onclick = falseFn; // disable link to prevent double-submit

In order for 'this' to refer to the element on which the onclick
handler has been placed, you must be attaching the function dynamically
using something like:

theLink.onclick = submitLinkOnclick;
Right.
var isOkToSubmit = false;
var form = document.getElementById("form");

I'm not a big fan of having an element with an ID the same as the tag
name, then also having a local variable with the same name - what
'form' refers to starts to get confusing. IE will add a 'form'
property to the global object too.
Neither am I, it was just something quick to type for the example.
// logic to see if it's OK to submit form (set isOkToSubmit = true)

if (isOkToSubmit) {

Since you changed isOkToSubmit to false above, this will always return
false and the form will never submit.
The comment was supposed to indicate that there would be logic in the
"real" function to determine if it's OK to submit the form.
form.submit();
} else {
alert("blah blah blah");
this.onclick = arguments.callee; // re-enable link

So you will always re-set the onclick to the current function. At what
point were you going to set it to falseFn?
It's the first thing that the function does.
}

return false;
}

The whole approach seems flawed. If you want to try something on the
client, put a submit button in the form and have a global variable (or
the value of some hidden form field) set to 'not submitted' (or false
or whatever). When the form is submitted, check the value to determine
whether to submit the form or not and change the value of the
variable/field to 'submitted' (or true or whatever). The following
example uses the form name to add a property to a global object so you
can keep track of multiple forms:

<script type="text/javascript">

var submittedForms = {};

function checkSubmit(formRef){
if (formRef.name in submittedForms){
alert('submitted');
return false;
}
submittedForms[formRef.name] = true; // Any value will do
}
</script>

<form name="formA" action=""
onsubmit="return checkSubmit(this);">
<!-- rest of form -->.
<input type="submit">
</form>
But it's not a very reliable method of stopping multiple submissions.
I don't have the option of replacing the link with a form control to do
the submission. In terms of preventing multiple submissions via the
link, do you see any technical issues with the
this.onclick=falseFn/this.onclick=arguments.callee combo?
--
Rob
Jul 26 '06 #3
ks********@gmail.com said the following on 7/26/2006 8:44 AM:
RobG wrote:
>ks********@gmail.com wrote:
<snip>
>The second (and probably bigger one) is that script execution on the
client is unreliable. You may end up with the form being submitted
multiple times anyway, or the user's first attempt to submit may not
work or be cancelled and your script may block subsequent submissions
unreasonably.

I'm not sure I follow. In what way is script execution unreliable? Do
you just mean that JavaScript could be disabled? Under what conditions
would the form be submitted multiple times?
The user could have it disabled, it could not be present at all (my
cellphone doesn't support JS), or, an error in the code of the page
could cause JS to stop executing.
>> // logic to see if it's OK to submit form (set isOkToSubmit = true)

if (isOkToSubmit) {
Since you changed isOkToSubmit to false above, this will always return
false and the form will never submit.

The comment was supposed to indicate that there would be logic in the
"real" function to determine if it's OK to submit the form.
That is one of the flaws of "typing an example" whereby the code that is
being reviewed isn't even close to a real example's code.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 26 '06 #4
Randy Webb wrote:
ks********@gmail.com said the following on 7/26/2006 8:44 AM:
RobG wrote:
ks********@gmail.com wrote:

<snip>
The second (and probably bigger one) is that script execution on the
client is unreliable. You may end up with the form being submitted
multiple times anyway, or the user's first attempt to submit may not
work or be cancelled and your script may block subsequent submissions
unreasonably.
I'm not sure I follow. In what way is script execution unreliable? Do
you just mean that JavaScript could be disabled? Under what conditions
would the form be submitted multiple times?

The user could have it disabled, it could not be present at all (my
cellphone doesn't support JS), or, an error in the code of the page
could cause JS to stop executing.
Disabled or not supported doesn't seem to be a concern for this app.
Not my call, just the reality of how the app is being developed for its
target environment. My only real concern is the implementation of the
submitLinkOnclick function. Assuming that the function is actually
invoked, are there any technical issues with it? I haven't come across
the approach of using this.onclick = falseFn/this.onclick =
arguments.callee to disable a link during processing, so I'm curious
about whether it's actually viable. One issue I can imagine, as noted
in the original post, is that it might make sense to use
try/catch/finally so that the link is re-enabled in case an exception
is thrown during the validation logic.
> // logic to see if it's OK to submit form (set isOkToSubmit = true)

if (isOkToSubmit) {
Since you changed isOkToSubmit to false above, this will always return
false and the form will never submit.
The comment was supposed to indicate that there would be logic in the
"real" function to determine if it's OK to submit the form.

That is one of the flaws of "typing an example" whereby the code that is
being reviewed isn't even close to a real example's code.
I suppose I could have used something like:

isOkToSubmit = validateForm(form);

without supplying the details of the validateForm function, but other
than that, the example actually is pretty close to the real code.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
2
by: lmeng | last post by:
Hi, I am new to this Forum. Thanks in advance for any kind help. In the following HTML code, when I change the value of one text field then click "Modify" button, if the validation fails a...
10
by: Steve Benson | last post by:
Our regular programmer moved on. I'm almost clueless in Javascript/ASP and got the job of adapting existing code. In the page below, everything works until I added the function checkIt() to...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
2
by: ddog | last post by:
I have a form with 3 text fields (one of which is a zip code) and 5 combo boxes. The combo boxes are all set with the first value as 'selected' when the page is first displayed. The 3 text fields...
12
by: Gustaf | last post by:
I've been working on a membership form for a while, and find it very tedious to get an acceptable level of form validation. A web search for solutions revealed some home-brewed solutions, such as...
6
by: smk17 | last post by:
I've spent the last few minutes searching for this question and I found an answer, but it wasn't quite what the client wanted. I have a simple online form where the user needs to fill out five...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.