Link to home
Start Free TrialLog in
Avatar of David Williamson
David WilliamsonFlag for United States of America

asked on

Clicking cancel on confirm prompt submits anyway

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?
Avatar of ho_alan
ho_alan

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">
Avatar of devic
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

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>
what a stupid careless mistake :-P....
that happens with me, too ;)
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">


ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<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
and i use onsubmit in the form tag