尝试调用按钮上的php函数单击以删除我正在循环的db中的行...需要将按钮标记id属性传递给该函数。不能让它工作
$('button').click(
function(){
var id = $(this).attr('id');
$.ajax({
url: "ins.php",
type: "POST",
data: id,
success: function(data){
}
});
});
// html button in table to delete a row
<td><button id='.$i.' name="delete"><img src="delete.png"></button></td>
// ins.php
if (isset($_POST['delete'])){
DB_delete($conn, $_POST['delete']);
}
// function that deletes the row
function DB_delete($conn,$id){
$sql = "DELETE FROM person WHERE id = ?";
$stmt=$conn->prepare($sql);
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->close();
}
答案 0 :(得分:1)
您根本没有正确传递数据。由于您正在寻找$ _POST [&#39;删除&#39;],因此您必须使用该键/值提交。
$.ajax({
url: "ins.php",
type: "POST",
data: {"delete": id},
success: function(data){
}
});
答案 1 :(得分:0)
ajax调用中的数据参数不正确。现在它只是一个简单的数字,但它应该是一个对象,以便在ins.php中被视为$ _POST或$ _GET(取决于'type'参数)。所以它应该是这样的:
$.ajax({
url: "ins.php",
type: "POST",
data: {"delete": id},
success: function(data){
}
});