【发布时间】:2016-04-17 02:45:13
【问题描述】:
我有一个 html 表格,其中显示列 - 客户姓名、员工姓名、事项和删除。我的删除列仅包含每一行的按钮。我的数据库包含列 id、日期、客户名称、员工姓名和事项。我的数据库不包含删除列,它只是显示在我的 html 表中。现在我想删除单击该行中相应删除按钮的特定行。只需检查我的 jquery 代码和 sql 代码,让我知道为什么它不起作用? jquery代码-
$(document).ready(function()
{
$('input[type=button]').click(function()
{
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).parent().parent().attr('id');
var data = 'id=' + id ;
var parent = $(this).parent().parent();
$.ajax(
{
type: "POST",
url: "delete.php",
data: data,
cache: false,
success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
});
我的sql代码-
<?php
include 'db.php' // DB Connection
if($_POST['id'])
{
$ID = mysql_escape_string($_POST['id']);
$sql = "DELETE FROM `newdata` WHERE id = '$ID'";
mysql_query($sql);
}
?>
我的html是这样的-
<table class="footable" data-filter="#filter" id="tableresult">
<thead>
<th>Client name</th>
<th>Staff name</th>
<th>Matter</th>
<th> Delete</th>
</thead>
<tr id="<?php echo $id; ?>" class="edit_tr">
<td class="edit_td" >
<span id="client_<?php echo $id; ?>" class="text"><?php echo $clientname; ?></span>
<input type="text" value="<?php echo $clientname; ?>" class="editbox" id="client_input_<?php echo $id; ?>" />
</td>
<td class="edit_td">
<span id="staff_<?php echo $id; ?>" class="text"><?php echo $staff; ?></span>
<input type="text" value="<?php echo $staff; ?>" class="editbox" id="staff_input_<?php echo $id; ?>"/>
</td>
<td class="edit_td">
<span id="matter_<?php echo $id; ?>" class="text"><?php echo $matter; ?></span>
<input type="text" value="<?php echo $matter; ?>" class="editbox" id="matter_input_<?php echo $id; ?>"/>
</td>
<div id ="delete_btn">
<td class="delete_td"><input type="button" id="del" class="btn btn-danger" value="×"></input></td>
</div>
</tr>
【问题讨论】:
-
检查错误和你的控制台
-
请不要使用
mysql_数据库扩展,它已被弃用(在 PHP7 中永远消失了)特别是如果您只是学习 PHP,请花精力学习PDO或mysqli_数据库扩展, and here is some help to decide which to use -
请stop using
mysql_*functions。 These extensions 已在 PHP 7 中删除。了解 PDO 和 MySQLi 的 prepared 语句并考虑使用 PDO,it's really pretty easy。 -
您的
$.ajax()中有语法错误,因为您在type: "POST";和url: 'delete.php';的末尾有一个分号;,它应该是一个逗号-type: "POST",和@ 987654341@。另外,$('#tableresult').removeRow(ID); ;中有一个额外的分号
标签: php jquery html mysql mysqli