473,714 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=fa lseFn/this.onclick=ar guments.callee combo and any
potential gotchas. Thanks.

function falseFn() {
return false;
}

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

var isOkToSubmit = false;
var form = document.getEle mentById("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.calle e; // re-enable link
}

return false;
}

Jul 26 '06 #1
4 2734
ks********@gmai l.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=fa lseFn/this.onclick=ar guments.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 submitLinkOncli ck() {
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 = submitLinkOncli ck;

var isOkToSubmit = false;
var form = document.getEle mentById("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.calle e; // 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(for mRef){
if (formRef.name in submittedForms) {
alert('submitte d');
return false;
}
submittedForms[formRef.name] = true; // Any value will do
}
</script>

<form name="formA" action=""
onsubmit="retur n checkSubmit(thi s);">
<!-- 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********@gmai l.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=fa lseFn/this.onclick=ar guments.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 submitLinkOncli ck() {
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 = submitLinkOncli ck;
Right.
var isOkToSubmit = false;
var form = document.getEle mentById("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.calle e; // 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(for mRef){
if (formRef.name in submittedForms) {
alert('submitte d');
return false;
}
submittedForms[formRef.name] = true; // Any value will do
}
</script>

<form name="formA" action=""
onsubmit="retur n checkSubmit(thi s);">
<!-- 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=fa lseFn/this.onclick=ar guments.callee combo?
--
Rob
Jul 26 '06 #3
ks********@gmai l.com said the following on 7/26/2006 8:44 AM:
RobG wrote:
>ks********@gmai l.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 26 '06 #4
Randy Webb wrote:
ks********@gmai l.com said the following on 7/26/2006 8:44 AM:
RobG wrote:
ks********@gmai l.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
submitLinkOncli ck 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.calle e 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(fo rm);

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.javas cript 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
4340
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 developers, but is relevant discussion. i'm not OT here unless someone thinks i'm trolling (which i'm not, obviously). then i'll disappear and never show my face again. :P
7
4892
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 because then it didn't work in IE. How can I enable this javascipt form validation to work in Netscape? When I use netscape, none of the alert boxes appear. It submits the form without validating anything.
2
1655
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 message will popup and the cotent of the form should NOT be submitted. (The actual code connects to the database at the backend so I can check if the value is submmited).
10
3585
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 validate which radio button was clicked and what was in a textfield. The form is an attendance checking page for a cyber charter school. What I'm trying to accomplish is that if a parent marks the student present, there should not be anything in the...
7
3614
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> </head> <style type="text/css">
27
4748
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
2
4270
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 are required and are by default empty. I need to validate that the text fields have an entry and that the zip code is numeric and the correct length. If the form fails validation - one of the text fields is empty, for example - I need to alert...
12
2492
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 these: http://simonwillison.net/2003/Jun/17/theHolyGrail/ http://samuelsjoberg.com/archive/2004/11/form-validation-on-client-and-server Quoting from the first link, this is my idea of what form validation is like from the user's perspective:
6
1868
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 fields out of nine. The other four are already there and filled out for the user. When they hit submit, all data is sent to us. But, if they desire (for whatever reason) the user can possibly delete what is already there and fill in something...
0
8808
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8712
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9077
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6637
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4727
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.