www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to confirm before deletion through a submit button

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    37

    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. #2
    Join Date
    Oct 2012
    Posts
    41
    I think you can shorten your function to:

    Code:
    function confirmBeforeDeletion()
    {
       return confirm("Are you sure to delete");
    }
    if your function returns true (the user selects 'Yes') the form will submit as normal, while if the form returns false the form will not submit. The minimum change you need to do is replace the 'return' in the code you posted with 'return false' i.e.:

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles