[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Clicking cancel on confirm prompt submits anyway

Posted on 2003-12-08
9
Medium Priority
?
829 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:David Williamson
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:ho_alan
ID: 9901186
what a stupid careless mistake :-P....
0
 
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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

834 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