Solved

Clicking cancel on confirm prompt submits anyway

Posted on 2003-12-08
9
818 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

820 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