使用jquery- $ .post();在php- foreach()函数内部,将变量数据发送到URL文件

时间:2016-09-07 07:22:08

标签: php jquery

我正在尝试删除文件夹内的文件,使用delete_file.php,这是动作文件offcourse。它只能将数据接收为$_POST['filename1'],但对于每次foreach()次迭代,此值都会被覆盖。任何建议,还有什么可以解决这个问题?在此先感谢。

问题:
delete_file.php将filename1作为$_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 . "&nbsp;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>

2 个答案:

答案 0 :(得分:0)

您可以尝试删除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>

更好的解决方案是用类替换id并将数据属性附加到magica

的值的元素上

答案 1 :(得分:0)

删除了所有AJAX-JQUERY代码&amp;使用:

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: &nbsp; <span style="color:red">'. $_GET['id']. '</span>,&nbsp; file from Directory</strong>');
    }
    else if(!unlink($filePath)){ 
    echo ("Error deleting file : ". $_GET['id']. " Already deleted OR doesn't EXIST"); 
    }

    ?>

像魅力一样工作!谢谢!