我怎么能给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()
{
}
});
和admin-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添加唯一后缀。