我在解决对PHP代码的ajax调用时遇到了一些困难。当我使用action="bannerDeleteItem.php"对代码进行硬编码时.php我能够调用bannerDeleteItem,但是当我尝试使用ajax调用转到bannerDeleteItem.php时,它只是刷新页面。我的代码中是否有任何错误?如果我错了,请纠正我,因为我仍然不熟悉 ajax 函数。谢谢
这是我的横幅.php代码:
<tbody>
<?php
include 'dbConnect.php';
global $conn;
$query = "SELECT * FROM I_Banner WHERE banner_kiosk ='1' ";
$result = sqlsrv_query($conn,$query);
while( $row = sqlsrv_fetch_array ( $result, SQLSRV_FETCH_ASSOC )){
?>
<tr>
<td><?php echo $row['banner_name']; ?></td>
<td><?php echo $row['banner_description']; ?></td>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="banner_id" value="<?php echo $row['banner_id']?>">
<td><center/><button id="delete" class="btn btn-info btn-sm" title="Remove"><i class="fa fa-times"></td>
</form>
</tr>
<?php
}
sqlsrv_free_stmt($result);
sqlsrv_close($conn);
?>
</tbody>
....
$(document).ready(function(){
//Delete banner item confirm box
$("button#delete").confirm({
confirm: function (el) {
$.ajax({
url: 'bannerDeleteItem.php',
data: el.closest('form').serialize(),
type: 'post',
}).done(function () {
//do something if you want when the post is successfully
if(!alert('Form Had Successfully Deleted.')){location.reload(); }
}).fail(function () {
//if the post is failed show an error message if you want
alert('Some error occur. Please try again later.');
}).always(function () {
//this is executed on success and failure
$('#myhiddendiv').hide();
})
}
});
这是我的横幅删除项目.php :
<?php
include 'dbConnect.php';
global $conn;
//Gather all required data
$item_id = $_POST['banner_id'];
//Delete file directory function
$query = "SELECT banner_data FROM I_Banner WHERE banner_id='$item_id'";
$result = sqlsrv_query($conn,$query);
while($row = sqlsrv_fetch_array ( $result, SQLSRV_FETCH_ASSOC )){
//Create the SQL query
$delete = "DELETE FROM I_Banner WHERE banner_id = '$item_id' ";
//Execute the query
$deleteResult = sqlsrv_query($conn,$delete);
$data = "uploads/$row[banner_data]";
unlink($data);
}
// Close the mysql connection
sqlsrv_close($conn);
// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>
横幅DeleleItem.php代码正在工作,只是当我按下"删除"按钮时,它会弹出确认框,我单击确定,但没有任何反应。似乎我的 ajax 代码没有继续使用 bannerDeleteItem.php 代码。我犯了什么错的地方?请纠正我
我已经通过在
它看起来像这样:
<td>
<form class="form" id="form" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="banner_id" value="<?php echo $row['banner_id']?>"></input>
<center/><button id="delete" class="btn btn-info btn-sm" title="Remove"><i class="fa fa-times"></i>
</form>
</td>
我错误地替换了表数据之外的表单,数据应该在表数据中提交。