2014-02-26 26 views
1

我有點小問題。我爲用戶提供了刪除數據庫記錄的選項。但首先我想爲他們提供一個jQuery確認框,詢問他們是否確定要刪除此特定記錄。Jquery確認框在取消選擇時仍然執行php代碼

問題是,當用戶點擊確認框中的取消我的PHP代碼仍然執行我的SQL刪除查詢。

我該如何停止執行取消選擇的代碼,並允許它在OK選擇上執行?

在此先感謝,我的代碼如下:

HTML

<div id="remove"> 
     <a href="myaccount.php?deletebook=' . $bookID . '" data-value="' . $customerName . '">delete</a> 
    </div> 

JQUERY

$(document).ready(function(){ 
     $('#remove a').click(function(){ 
      var customerName = $(this).data('value'); 
      return confirm("Do you really want to delete your book with the name: "+customerName+"?"); 
     }); 
    }); 

PHP

 if(isset($_GET['deletebook'])){ 
     $bookID = trim(strip_tags($_GET['deletebook'])); 
     $bookID = mysql_real_escape_string($bookID); 

     $sql50 = "DELETE FROM books WHERE bookID='$bookID' LIMIT 1"; 
     $result50 = mysql_query($sql50) or die(mysql_error()); 

     $sql51 = "DELETE FROM pages WHERE bookID='$bookID'"; 
     $result51 = mysql_query($sql51) or die(mysql_error()); 

     header("location: myaccount.php"); 
    } 

回答

3

更改您的jquery這樣:

$(document).ready(function() { 
    $('#remove a').click(function (e) { 
     var customerName = $(this).data('value'); 
     var r = confirm("Do you really want to delete your book with the name: " + customerName + "?"); 
     if (r != true) { 
     e.preventDefault(); 
     } 

    }); 
}); 
+0

沒有工作仍然執行php代碼,它需要讀$('#刪除a')。click(function(e){可能? –

+0

對不起,它現在應該可以工作,我忘了通過點擊處理程序傳遞事件變量 –

+0

Z Spence您的編輯工作就像一個魅力,我感謝您的幫助和時間,幫助 –

0

你的代碼工作正常,看fiddle

$(document).ready(function(){ 
    $('#remove a').click(function(){ 
     var customerName = $(this).data('value'); 
     return confirm("Do you really want to delete your book with the name: "+customerName+"?"); 
    }); 
}); 
0
$(document).ready(function() { 
    $('#remove a').click(function() { 
     var customerName = $(this).data('value'); 
     if(confirm("Do you really want to delete your book with the name: " +customerName+ "?")){ 
      // YOUR DELETE CODE 
     } 
    }); 
}); 
0

如果使用,普通的JavaScript確認框,你的代碼應該工作正常。共享相關的插件,如果你使用任何轉換原生js確認框到自定義的

相關問題