SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Confirm box delets row even after pressing "Cancel"

    Hi all.I found this code on a forum to confirm that the user wants to delete the record from database and that the link has not pressed accidently.But it delets the record even after pressing the cancel button.Can anyone tell why?Do I need to modify the javascript function?Here is my code:

    Code:
    <script type="text/javascript">
    <!--
    function confirmation() {
    	var answer = confirm("Delete Record?")
    	if (!answer){
    		
    		window.location = "by_hand.php";
    		}
    }
    //-->
    </script>
    Code:
    <a href=delete.php?delete=$id&&action=deleteemployee onclick='confirmation()'><img src='images/delete.png' alt='Delete' Title='Delete'class='pngfix'></a>
    It's easy once you know how...

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    can't it just be
    function confirmation() {
    if !(confirm("Delete Record?")) {
    return false;
    }
    }
    ?

    if the user hits OK, then confirm("blah") is True and the url you have should run.
    if the user hits CANCEL, confirm("blah") is not true and we check for !confirm() and if so, return false, which stops browser action of the clicking.


    Notes:
    Code:
    <script type="text/javascript">
    <!--
    function confirmation() {
    	var answer = confirm("Delete Record?")
    	if (!answer){
    		
    		window.location = "by_hand.php";
    		}
    }
    //-->
    </script>
    Get rid of the stuff in red, you're not writing for Netscape 4 or IE5 anymore.

    Code:
    <a href='delete.php?delete=$id&amp;&&action=deleteemployee' onclick='confirmation()'><img src='images/delete.png' alt='Delete' Ttitle='Delete'spacebarclass='pngfix'></a>
    Quote all your attribute values, escape your ampersands, keep spaces between your attributes, and remember that capitalisation matters (so generally lowercase everything in code).
    I also would not put the title on the img. I'd probably have no title at all if you you are going to use one, put it on the anchor. The image already has an alt attribute.

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can the following JS code

    Code:
    function confirmation() {
    	var answer = confirm("Delete Record?")
    	if (!answer){
    		
    		window.location = "by_hand.php";
    		}
             else
             {
                   window.location = "delete.php?delete=$id&action=deleteemployee";
              }
    }
    and put "#" into href attribute.

    Then you delete your record only when you answer OK. In your case you may call the link in href earlier than in window.location, so, the record will be deleted.

  4. #4
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your replies Stomme poes and Alex69
    It's easy once you know how...

  5. #5
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alex69 View Post
    Or you can the following JS code

    Code:
    function confirmation() {
    	var answer = confirm("Delete Record?")
    	if (!answer){
    		
    		window.location = "by_hand.php";
    		}
             else
             {
                   window.location = "delete.php?delete=$id&action=deleteemployee";
              }
    }
    and put "#" into href attribute.

    Then you delete your record only when you answer OK. In your case you may call the link in href earlier than in window.location, so, the record will be deleted.
    I putted above code and it works fine now.It dosent delete the record.But it is not deleting the record even after pressing ok.This is may be because the id is not passed properly.As I am not too good with js, can you please tell me how to pass php variable through window.open or href?
    It's easy once you know how...


Bookmarks

Posting Permissions

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