Javascript 在php foreach中使用jquery动态选择器删除单个图像
我试图删除文件夹中的图像,在那里我使用PHPJavascript 在php foreach中使用jquery动态选择器删除单个图像,javascript,php,jquery,Javascript,Php,Jquery,我试图删除文件夹中的图像,在那里我使用PHPglob()单独访问它们&然后使用jquery leanmodel弹出确认窗体,然后通过$.post()发送请求,这样页面就不需要刷新 但是,它不起作用 问题 猜猜看,我哪里做错了 我想做的事 我正在使用glob从文件夹中抓取所有图像。然后我将它们显示在表格的网页中。在表中,我将每个图像链接到一个删除选项(通过href)。要删除图像,用户单击运行jQuery脚本的链接,该脚本将显示确认弹出表单。确认后,a$.post()查询运行,指向包含删除链接文件/
glob()
单独访问它们&然后使用jquery leanmodel
弹出确认窗体,然后通过$.post()
发送请求,这样页面就不需要刷新
但是,它不起作用
问题
猜猜看,我哪里做错了
我想做的事
我正在使用glob从文件夹中抓取所有图像。然后我将它们显示在表格的网页中。在表中,我将每个图像链接到一个删除选项(通过href
)。要删除图像,用户单击运行jQuery脚本的链接,该脚本将显示确认弹出表单。确认后,a$.post()代码>查询运行,指向包含删除链接文件/图像的代码的url
正在发生的事情/输出
当用户单击链接时,URL会更改为,例如,.php#imagename_jpg
,但不会显示弹出式确认表单
代码在没有foreach的情况下运行良好
注意:我已经将代码中的所有注释放在了行中。我希望现在很容易理解
<?php
$dirname = "../assets/img/logos/";
$images = glob($dirname."*.{jpg,png,gif,tiff,jpeg,JPG}", GLOB_BRACE); //using PHP `glob()` to access images inside path, individually
foreach($images as $image) {
$variable_id = str_replace(".", "_", $image); // creating a dynamic id, from each image-name
?>
// below starting ,code to link - to delete/unlink this image file on clicking `id=modaltrigger`, followed by a pop-up confirmation form.
<p style="text-align: center; font-size: 10px; margin-top: 5px;">
<a id="modaltrigger_<?=$variable_id ?>" href="#<?=$variable_id ?>" class="btn" style="border: none !important;">Delete</a>
</p> // i hope this code is simple to understand
// pop-up confirmation form
<div id="<?=$variable_id ?>" class="popupContainer" style="display:none;">
<div class="deleteplogo_<?=$variable_id ?>">
// some form code to pop-up with submit button & `action=post`
</div>
</div>
<!-- jquery : delete image confirmation: pop up based on leanModal jquery UI -->
<script type="text/javascript">
var magica = <?php echo $variable_id; ?>; // using php variable in jquery
$('#modaltrigger_'+magica).leanModal({top : 200, overlay : 0.6, closeButton: ".modal_close" }); // variable selector
$(function() {
// Calling popUp delete Form on clicking variable-id of ahref above
$('#modaltrigger_'+magica).click(function() {
$('.deleteplogo_'+magica).show(); // showing up hidden class above
//$(".header_title").text('Are you sure , you want to delete?');
return false;
});
})
</script>
<?php
}
?>
//在“开始”下面,单击“id=modaltrigger”删除/取消链接此图像文件的代码,然后单击弹出的确认表单。
//我希望这段代码简单易懂
//弹出确认表
删除所有AJAX-JQUERY代码并使用:
file1.php
<td>
<p style="text-align: center; font-size: 10px; margin-top: 5px;">
<a id="modaltrigger_<?= $variable_id ?>" href="delete.php?id=<?php echo $imageName; ?>" class="btn"
style="border: none !important;">Delete</a>
</p>
</td>
delete.php
<?php
$filePath = "D:/folder/".$_GET['id'];
if(is_file($filePath)){
@unlink($filePath);
echo ('<strong>SUCCESS! Deleted: <span style="color:red">'. $_GET['id']. '</span>, file from Directory</strong>');
}
else if(!unlink($filePath)){
echo ("Error deleting file : ". $_GET['id']. " Already deleted OR doesn't EXIST");
}
?>
它做了什么不应该做的事?它有什么不应该做的呢?在没有任何线索的情况下转储一块代码不是很有帮助。一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。您可能会被要求在几周/几个月内修改此代码,最终您将感谢我。@RiggsFolly我已将所有注释在代码行中。我希望现在很容易理解。我已经详细解释了每一点。更改变量\u id。这是导致问题的原因。因为它包含整个图像名称和路径。将其更改为简单数字并选中.foreach($images as$image){$imageName=str_replace(“path”、“”、$image);$variable_id=str_replace(“.”、“”、$imageName);