2016-02-05 38 views
-4

我想獲取表格上一行的id,以便一旦點擊了該行的刪除按鈕,該行就會被刪除。請幫忙。如何獲取表格上一行的ID

$sql = "SELECT * FROM `users`"; 
$result = $conn->query($sql); 

if($result !== false) { 

echo "<form method='post' action=''>"; 
foreach($result as $row) { 

    echo $row['id']. ' - '. $row['user_email']. ' - '. $row['user_password']. ' - '. $row['user_username']; 

    echo "<button type='submit' class='btn btn-link'>Delete</button><br>"; 
echo "</form>"; 


} 

}

+1

請你嘗試! –

+1

在這裏添加您的代碼! –

+0

你的桌子是怎樣的? – osanger

回答

0
<table id="example"> 
    <thead> 
    <tr> 
     <th>name</th> 
     <th>age</th> 
     <th></th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr id="row11"> 
     <td>murali</td> 
     <td>25</td> 
     <td><button>get data</button></td> 
    </tr> 
    <tr id="row12"> 
     <td>murali2</td> 
     <td>24</td> 
     <td><button>get data</button></td> 
    </tr> 
    </tbody> 
</table> 
<script src="js/jquery.min.js"></script> 
<script> 
    $('#example tbody').on('click', 'button', function() { 
     var btn = $(this); 
     if(btn){ 
      var row = $(this).closest('tr'); 
      console.log(row[0]); 

     } 
    }) 
</script>