stay in the current page after clicking cancel button

28,876

Solution 1

Add following line in the else part:

return false;

and change your onclick to:

return confirmation();

=== UPDATE ===

Because you have the confirmsubmit.jsp in the form action, you don't need the window.location:

function confirmation() {
    if (!confirm("Confirm submit?")) {
        return false;
    }
}

Also see this example.

Solution 2

 <form action="confirmsubmit.jsp" method="POST">
    <script type="text/javascript">
    <!--
    function confirmation() {
    var answer = confirm("Confirm submit?")
    if (answer){

        window.location = "confirmsubmit.jsp";// goes to confirmsubmit.jsp
        return true;
    }
    else{
        //should remain in index.jsp but here also confirmsubmit.jsp is opening
    return false;
    }
    }
    //-->
    </script>
    <input type="text" name="textboxname"/>
    <input type="submit" onclick="return confirmation()"/> 

</form>  
Share:
28,876

Related videos on Youtube

Tom
Author by

Tom

Updated on July 22, 2022

Comments

  • Tom
    Tom almost 2 years

    I have a form in index.jsp and after clicking submit i am showing an alert "confirm submit?" if ok will be clicked then confirmsubmit.jsp will be displayed. I am getting text box name in confirmsubmit.jsp by request.getParameter("textboxname");But problem is if I click cancel then also confirmsubmit.jsp is opening, how can I stay in index.jsp after clicking cancel button in alert?

    Any help please

    index.jsp

    <form action="confirmsubmit.jsp" method="POST">
    <script type="text/javascript">
    <!--
    function confirmation() {
    var answer = confirm("Confirm submit?")
    if (answer){
    
        window.location = "confirmsubmit.jsp";// goes to confirmsubmit.jsp
    }
    else{
        //should remain in index.jsp but here also confirmsubmit.jsp is opening
    }
    }
    //-->
    </script>
    <input type="text" name="textboxname"/>
    <input type="submit" onclick="confirmation()"/> 
    </form> 
    
  • Tom
    Tom about 12 years
    thanks scessor for quick answer but i am still able to view confirmsubmit.jsp after clicking cancel button
  • scessor
    scessor about 12 years
    The result must be returnd from the onclick too.
  • Tom
    Tom about 12 years
    yeah it worked thanks a lot scessor, we met after a long time
  • scessor
    scessor about 12 years
    I've added a shorter solution.