Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to confirm before deletion through a submit button

    Hi,

    I have a submit button which deletes some checked out records on my web page.

    Now I want to add a messagebox before deleting the records, but somehow it is not working.
    Upon selection of either of 'ok' and 'cancel' , it is deleting the record. How can I make sure that
    delete only when 'ok' is pressed and 'Do not' delete when 'cancel' is pressed ?

    Following is my code :

    Code:
    $cgi->submit(-label => "Remove selected records", -onClick => 'confirmBeforeDeletion()');
    
    function confirmBeforeDeletion 
    {
       var con = confirm("Are you sure to delete");
       if(con == true)
       {  
          form.submit;
       }
       else
       {
          return;
        }
    }
    These above javascript function is not taking the appropriate action which I want. In both cases if and else, it is deleting the record.

    Please help.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    return false;


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I tried with retun false;

    But it seems to be not working.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    function confirmBeforeDeletion () {

    It is recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage. I think it contributed to this error.
    Last edited by Philip M; 03-07-2013 at 01:03 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi ,

    I have changed the code to this following where the '{' are put beside the function and if else block names and also every statement has a ';' at the end. But it is still not working :

    Code:
    function confirmBeforeDeletion () {
    
       var con = confirm("Are you sure to delete");
       if(con == true) {
       
          form.submit;
       }
       else {
       
          return;
        }
    }

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    return by itself does not return any value. You should use return false; as suggested previously to prevent the form from submitting.

    And form.submit should be form.submit().
    Is "form" correctly defined? Try document.getElementById("formid").submit();
    Assigning a name to a form is deprecated and obsolete.
    Last edited by Philip M; 03-08-2013 at 08:50 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    return by itself does not return any value. You should use return false; as suggested previously to prevent the form from submitting.

    And form.submit should be form.submit(). Is "form" correctly defined? Try document.getElementById("formid").submit();
    Assigning a name to a form is deprecated and obsolete.
    Sorry for the mistyping. In my code actually did 'return false' but forgot to mention it here . But it is not working. It goes to the else block as I can track by an alert message , but the result is not as expected.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Quote Originally Posted by xaverian View Post
    Sorry for the mistyping. In my code actually did 'return false' but forgot to mention it here . But it is not working. It goes to the else block as I can track by an alert message , but the result is not as expected.
    onClick => 'confirmBeforeDeletion()');

    Surely onClick should be onsubmit =

    Show us what you HTML actually looks like.
    Last edited by Philip M; 03-08-2013 at 09:19 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Following is the HTML :

    Code:
    <input type=hidden name=actiondel value="delete">
      <div id="divCheckbox" style="visibility:hidden"> <input type="submit" name=".submit" value="Remove selected Cell Collections" onclick="confirmBeforeDeletion()" /></div> <BR><hr>
    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
      <TR>
        <TD WIDTH=42%>
          <FONT SIZE=-2>&copy; Copyright(c) 1996 - 2013 by nds <BR>
        reserved.</FONT></TD>
        <TD WIDTH=42%>
          <FONT SIZE=-2>Last modified Fri Mar  8 10:36:00 MET 2013 <BR>
                    by <A HREF="mailto:nil@nds.com">Nil</A>
        </TD>
          </TR>
    </TABLE>
    <p /><br />
    </body>
    </html>

  • #10
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by xaverian View Post
    Following is the HTML :
    It'd probably be more helpful if you could show the HTML for your form instead of HTML for two input elements that aren't part of a form.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #11
    New Coder
    Join Date
    May 2012
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well it is working now. In addition with 'return false', I have also done 'return confirmBeoforeDeletion' in the onclick of my submit function.

    Code:
    <input type="submit" name=".submit" value="Remove selected Cell Collections" onclick="return confirmBeforeDeletion()"
    Thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •