Solved

Clicking cancel on confirm prompt submits anyway

Posted on 2003-12-08
9
823 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

717 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