[英]Delete records in AJAX
我在使用AJAX的“删除”按钮中单击后试图删除行的记录,我尝试了以下操作,但它给了我一个错误:
//index.php
$(document).on('click', '.delete', function(){
var Serial = $(this).attr("Serial");
if(confirm("Are you sure you want to remove this data?"))
{
var action = "Delete";
$.ajax({
url:"plans-action.php?square=$selsquare",
method:"POST",
data:{Serial:Serial, action:action},
success:function(data)
{
fetchUser();
alert(data);
}
})
}
else
{
return false;
}
});
});
//plans-fetch.php
这是我在服务器端php代码中所做的
if(isset($_POST["action"]))
{
if($_POST["action"] == "Delete")
{
$statement = $connection->prepare(
"DELETE FROM plans WHERE SquareNo = '$selsquare' AND Serial = :Serial"
);
$result = $statement->execute(
array(
':Serial' => $_POST["Serial"]
)
);
if(!empty($result))
{
echo 'Data Deleted';
}
}
}
使用POST
时,不必将参数附加到url的末尾。 在$.post
函数的data部分中包含参数。 建议使用data-
使用自定义属性时,您的串行属性应类似于data-serial="somevalue"
而不是Serial="somevalue"
$(document).on('click', '.delete', function(e){
e.preventDefault();
// using data-serial
// var serial = $(this).data('serial');
var serial = $(this).attr("Serial");
if(confirm("Are you sure you want to remove this data?"))
{
var action = "Delete";
var selsquare = <?php echo $selsquare; ?>; // echo value from php
$.ajax({
url:"plans-action.php", // url:"plans-action.php?square=$selsquare" we don't need params
method:"POST",
data:{Serial:serial, action: action, selsquare: selsquare },
success:function(data)
{
fetchUser();
alert(data);
}
})
}
else
{
return false;
}
});
然后在您的PHP
if (isset($_POST["action"])) {
if ($_POST["action"] == "Delete") {
$selsquare = $_POST['selsquare'];
$statement = $connection->prepare("DELETE FROM plans WHERE SquareNo = '$selsquare' AND Serial = :Serial");
$result = $statement->execute(array(':Serial' => $_POST["Serial"]));
if (!empty($result)) {
echo 'Data Deleted';
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.