Solved

Clicking cancel on confirm prompt submits anyway

Posted on 2003-12-08
9
814 Views
Last Modified: 2011-09-20
I have the following script:
<script type="text/javascript">
function confirmOverwrite(question) {
      var do_ovrw = window.confirm(question)
      if (do_ovrw) {
            document.modClient.submit();
            }
}
</script>

When the cancel button is clicked, the form submits anyway.  Here is the related button code:

<input type="submit" name="SaveChanges"
onClick="javascript:confirmOverwrite('Are you sure you want to overwrite this record?')"
value="Save Changes">

Why is the form still submitting?
0
Comment
Question by:theamzngq
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:ho_alan
ID: 9901065
u may try the following
use onSubmit, rather than onClick here

<script type="text/javascript">
function confirmOverwrite(question) {
    var do_ovrw = window.confirm(question)
    return do_overw;
}
</script>

<input type="submit" name="SaveChanges"
onSubmit="return confirmOverwrite('Are you sure you want to overwrite this record?')"
value="Save Changes">
0
 
LVL 25

Expert Comment

by:devic
ID: 9901115
change type="submit" to type="button"

or

onSubmit="return confirmOverwrite('Are you sure you want to overwrite this record?')"
like sad ho_alan, but move this to form tag

0
 
LVL 5

Expert Comment

by:ho_alan
ID: 9901182
oh yes!!! thx for pointing me out!


<form name=modClient onSubmit="return confirmOverwrite('Are you sure you want to overwrite this record?')" method=post action="yourdest.asp">

<input type="submit" name="SaveChanges" value="Save Changes">
</form>
0
 
LVL 5

Expert Comment

by:ho_alan
ID: 9901186
what a stupid careless mistake :-P....
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Expert Comment

by:devic
ID: 9901238
that happens with me, too ;)
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9901507
confim returns true if OK is clicked and false if CANCEL is clicked. There is no need for the function  confirmOverwrite(), you can merely write this


<input type="submit" name="SaveChanges"
onSubmit="confirm('Are you sure you want to overwrite this record?')" value="Save Changes">


0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 100 total points
ID: 9901744
xorry should be

<input type="submit" name="SaveChanges"  onClick="return confirm('Are you sure you want to overwrite this record?')"  value="Save Changes">
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9901889
<script type="text/javascript">
function confirmOverwrite(question){
    if(confirm(question)){
        return true;
    }
     else {
             return false;
     }
}
// -->
</script>

and then in the form tag use onsubmit

<form name="form1" action="" onsubmit="return confirmOverwrite('Are you sure you want to overwrite this record?');">


This is basically the same as GwynnforWeb's solution with the exception of i use a function
So Credits to her
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9901894
and i use onsubmit in the form tag
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now