使用jquery-$.post();在php-foreach()函数中,将变量数据发送到URL文件
我试图删除文件夹中的文件,使用delete_file.php,这是一个动作文件。它只能以使用jquery-$.post();在php-foreach()函数中,将变量数据发送到URL文件,php,jquery,Php,Jquery,我试图删除文件夹中的文件,使用delete_file.php,这是一个动作文件。它只能以$\u POST['filename1']的形式接收数据,但对于每一次foreach()迭代,该值都会被重写。有什么建议可以解决这个问题吗?提前谢谢 问题: delete_file.php正在将filename1捕获为$\u POST['filename1'] 但是,每次foreach迭代,filename1都会被重写。 而delete_file.php,每次只删除最后一个条目 <!DOCTYP
$\u POST['filename1']
的形式接收数据,但对于每一次foreach()
迭代,该值都会被重写。有什么建议可以解决这个问题吗?提前谢谢
问题:delete_file.php
正在将filename1捕获为$\u POST['filename1']
但是,每次foreach迭代,filename1都会被重写。
而delete_file.php
,每次只删除最后一个条目
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!--[if lte IE 8]>
<script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assets/css/main.css"/>
<!--[if lte IE 8]>
<link rel="stylesheet" href="assets/css/ie8.css"/><![endif]-->
<!--[if lte IE 9]>
<link rel="stylesheet" href="assets/css/ie9.css"/><![endif]-->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.leanModal.min.js"></script>
</head>
<body class="no-sidebar">
<table border="1px solid" style="text-align:center; margin-left:auto; margin-right:auto; width:900px">
<tr>
<th><strong>S.No</strong></th>
<th><strong>Image-Name</strong></th>
<th><strong>Image/Logo</strong></th>
<th><strong>Image Size</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
$dirname = "../assets/img/logos/";
$images = glob($dirname . "*.{jpg,png,gif,tiff,jpeg,JPG}", GLOB_BRACE);
$sn = 1;
foreach ($images as $image) {
$imageName = str_replace("../assets/img/logos/", "", $image);
$variable_id = str_replace(".", "_", $imageName);
?>
<!---///////////////////// IMAGE-<?= $sn++ ?> STARTS //////////////////////----------->
<tr>
<td><?= $sn++ ?></td>
<td><?php echo $imageName; ?></td>
<td><img src="<?php echo $image; ?>" width="150px" height="100px"/></td>
<td><?php echo filesize($image) / 1000 . " KB"; ?></td>
<!-------
<td>
<a id="delete" href="delete_plogos.php?filename=<?//=$imageName ?>" style="color:#D00A0D"><strong>Delete</strong></a>
</td>
-------->
<td>
<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>
</td>
</tr>
<!----------------------popup for delete----------------------->
<div id="<?= $variable_id ?>" class="popupContainer" style="display:none;">
<header class="popupHeader" style="background: #F4F4F2;
position: relative;
padding: 10px 20px;
border-bottom: 1px solid #DDD;
font-weight: bold; height: 55px;">
<span class="header_title">Delete</span>
<span class="modal_close"><i class="fa fa-times"></i></span>
</header>
<section class="popupBody">
<!-- Register Form -->
<div class="deleteplogo_<?= $variable_id ?>">
<form id="newpageform_<?= $variable_id ?>" name="newpageform_<?= $variable_id ?>" method="post">
<input name="filename_<?= $variable_id ?>" id="filename_<?= $variable_id ?>" type="text" style="display:none"
value="<?= $imageName ?>"/>
<p><strong><?= $imageName ?> - Image will be deleted Permanently. <br/>Proceed
?</strong></p>
<br/>
<div class="action_btns">
<div class="one_half" style="float:none;">
<a id="ajax-submit_<?= $variable_id ?>" class="btn btn_red" style="cursor: pointer"
>Yes! I
Agree
.Delete</a></div>
</div>
</form>
</div><!--------delete_plogo----------->
</section>
</div> <!----------------------#modal ENDS----------------------->
<!---------------- delete image : pop up ------------------------------>
<script type="text/javascript">
var magica = "<?php echo $variable_id; ?>";
$('#modaltrigger_' + magica).leanModal({top: 200, overlay: 0.6, closeButton: ".modal_close"});
$(function () {
$('#modaltrigger_' + magica).click(function () {
$('.deleteplogo_' + magica).show();
return false;
});
})
/////// 3.) AJAX-FORM SUBMIT - + then reload
$("#ajax-submit_"+magica).click(function () {
var filename = $("#filename_"+magica).val();
if (filename == '') {
alert("File doesn't EXIST....!!");
} else {
$.post("delete_plogos_action.php", {filename1: filename}, function (data) {
$("span.modal_close > i").trigger("click"); // to auto-close leanModal window
alert(data).fadeOut("slow");
window.location=trustedpartners_listviewdel_logos.php;
//close_modal("modal");
});
}
});
</script>
<!---///////////////////// IMAGE-ENDS //////////////////////----------->
<?php
}
?>
</table>
</body>
</html>
S.No
图像名称
图像/徽标
图像大小
行动
“width=“150px”height=“100px”/>
您可以尝试删除php循环:
<script type = "text/javascript" >
$(function() {
$('div[id^="modaltrigger_"]').leanModal({
top: 200,
overlay: 0.6,
closeButton: ".modal_close"
});
$('div[id^="modaltrigger_"]').click(function() {
magica = $(this).attr('id').split('_')[1];
$('.deleteplogo_' + magica).show();
return false;
});
})
$('div[id^="ajax-submit_"]').click(function() {
magica = $(this).attr('id').split('_')[1];
var filename = $("#filename_" + magica).val();
if (filename == '') {
alert("File doesn't EXIST....!!");
} else {
$.post("delete_file.php", {
filename1: filename
}, function(data) {
$("span.modal_close > i").trigger("click");
//alert(data).fadeOut("slow"); damn you can't fade out a alert box :))
window.location = mypage.php;
} // else ends here
});
}); < /script>
$(函数(){
$('div[id^=“modaltrigger_”]”)。leanmodel({
前200名,
叠加:0.6,
关闭按钮:“.modal_close”
});
$('div[id^=“modaltrigger_quo;])。单击(函数(){
magica=$(this.attr('id').split('uu')[1];
$('.deleteplogo_'+magica).show();
返回false;
});
})
$('div[id^=“ajax-submit\”)。单击(函数(){
magica=$(this.attr('id').split('uu')[1];
var filename=$(“#filename_uquo+magica).val();
如果(文件名=“”){
警报(“文件不存在….!!”;
}否则{
$.post(“delete_file.php”{
filename1:filename
},函数(数据){
$(“span.modal_close>i”)。触发器(“单击”);
//警报(数据)。淡出(“慢”);该死,你不能淡出警报框:)
window.location=mypage.php;
}//否则就到此为止
});
});
更好的解决方案是用类替换ID,并向元素i附加一个数据属性,其值为magica
删除所有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");
}
?>
而不是循环,收集所有要删除的文件名
,然后发送一次。为什么在phpforeach
循环中有
?代码中有错误。是否缺少任何内容?我怀疑这是您的实际代码,您缺少一些标记,这些标记会导致代码抛出错误很多错误,请向我们展示您的实际代码,而不是伪代码。首先,不要将js与php混合使用是一个糟糕的想法。当您在ajax之后重新加载页面时,您刚刚破坏了ajax应该做什么的想法,为什么不做一个简单的post请求?如果我删除php foreach循环,我如何从文件夹中检索数据?