2014-10-20 61 views
1

我對php,html,javascript,jquery和sql有一些瞭解。我不知道如何去做這件事,但我真正想做的是能夠從SQL表中拉出數據(我已經知道如何做到這一點)。然後用旁邊的按鈕顯示數據(我也已經知道該怎麼做)。當他們點擊按鈕時,它將刪除包含可見數據的元素,併發送一個sql語句來刪除數據庫中的行。視圖非常粗糙的代碼。刪除sql row使用按鈕而不刷新頁面

<?php 
echo '<p>Data 1: '".$someDataFromDB."'<button id='deleteThisRow'></button></p>; 
?> 

從這個我想:

  1. 點擊按鈕
  2. 刪除段落元素,使他們的用戶都知道它已被刪除
  3. 然後發送SQL語句從刪除的行sql表

所有這些都不刷新頁面。這是否可能?

+2

是。你需要搜索ajax。 – jeroen 2014-10-20 14:29:32

+0

你不應該「發送一個sql語句」。 YOu可以向服務器發送一條指令,告訴它刪除一條特定的記錄,但是你的客戶端應用程序不應該在sql級別處理任何事情。考慮有人搞清楚你的​​「run sql」查詢語法,並通過http://example.com/dosql.php?sql=drop數據庫databasename()'「oops」發送。 – 2014-10-20 14:36:27

+0

「發送一條sql語句」我認爲它是不言而喻的,一個sql查詢將是PDO或mysqli。 – 2014-10-20 14:47:30

回答

2

是的,這是可能的,但你應該只發送條目的id到一個特定的頁面來刪除entry.never整個sql語句。

<button id="<?php echo $row['id']; ?>" class="delbutton"></button> 

這個腳本添加到您的網頁

<script type="text/javascript" > 
     $(function() { 

      $(".delbutton").click(function() { 
       var del_id = $(this).attr("id"); 
       var info = 'id=' + del_id; 
       if (confirm("Sure you want to delete this post? This cannot be undone later.")) { 
        $.ajax({ 
         type : "POST", 
         url : "delete_entry.php", //URL to the delete php script 
         data : info, 
         success : function() { 
         } 
        }); 
        $(this).parents(".record").animate("fast").animate({ 
         opacity : "hide" 
        }, "slow"); 
       } 
       return false; 
      }); 
     }); 
</script> 

最後,但沒有起碼你的刪除腳本

<?php 
include 'connection.php'; 
$id=$_POST['id']; 
$delete = "DELETE FROM table WHERE id=$id"; 
$result = mysql_query($delete) or die(mysql_error()); 
?> 
+0

完美。必須在按鈕上添加onclick =「」才能使腳本運行 – 2014-10-20 17:36:53