使用 ajax 删除使用 glob() 显示的图像
remove image that show with glob() with ajax
我怎样才能给 div.pix-box 一个增量特定的 id,比如 pix-1 , pix-2 ,...
为了用 ajax?
删除它们
$images = glob("user/$username/"."*.{jpg,jpeg,gif,png}",GLOB_BRACE);
foreach($images as $image) {
echo '<div class="pix-box"><img src="'.$image.'" />'.'<br>';
echo '<form class="remover" action="admin-delete.php" method="post">';
echo '<input type="hidden" value="'.$image.'" name="delete_file" />';
echo '<input type="submit" name="submit" value="Delete image" />';
echo '</form>';
echo '</div>';
}
这是我的 ajax 脚本:
$(".remover").on('submit',(function(d) {
d.preventDefault();
$.ajax({
url: "admin-delete.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$('#pix-....').remove();
},
error: function()
{
}
});
和管理员-delete.php:
<?php
if (array_key_exists('delete_file', $_POST)) {
$filename = $_POST['delete_file'];
if (file_exists($filename)) {
unlink($filename);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
?>
添加增量变量:
$i = 1;
foreach($images as $image) {
...
$i++;
}
现在您可以使用 $i
为您想要的任何 ID 添加唯一后缀。
我怎样才能给 div.pix-box 一个增量特定的 id,比如 pix-1 , pix-2 ,... 为了用 ajax?
删除它们$images = glob("user/$username/"."*.{jpg,jpeg,gif,png}",GLOB_BRACE);
foreach($images as $image) {
echo '<div class="pix-box"><img src="'.$image.'" />'.'<br>';
echo '<form class="remover" action="admin-delete.php" method="post">';
echo '<input type="hidden" value="'.$image.'" name="delete_file" />';
echo '<input type="submit" name="submit" value="Delete image" />';
echo '</form>';
echo '</div>';
}
这是我的 ajax 脚本:
$(".remover").on('submit',(function(d) {
d.preventDefault();
$.ajax({
url: "admin-delete.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$('#pix-....').remove();
},
error: function()
{
}
});
和管理员-delete.php:
<?php
if (array_key_exists('delete_file', $_POST)) {
$filename = $_POST['delete_file'];
if (file_exists($filename)) {
unlink($filename);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
?>
添加增量变量:
$i = 1;
foreach($images as $image) {
...
$i++;
}
现在您可以使用 $i
为您想要的任何 ID 添加唯一后缀。