duande9301 2017-03-31 20:54
浏览 48
已采纳

如何使用复选框从网格中选择和删除文件

I am currently working on a Directory Browser using PHP and I've stumbled upon a roadblock. I would like to be able to have a user select files using checkboxes, then delete them when they click a button, however I'm not sure how to go about this due to being new to PHP and web development in general. Here is my code so far for the user interface:

                            <a href="#" onClick="rec('deletestuff')">
                            <span title="Delete Selected" class="fa fa-trash fa-lg"></span>
                            <!--Delete Selected-->
                        </a>
                    </li>
                </ul>
                <script type="text/javascript">
                    function rec(deletestuff) {
                        $('#newCode').load('delete_selected.php' deletestuff);
                    }
                </script>
<span class="selected col-md-2 col-sm-2 col-xs-1 text-right">
                                <form method="get">
                                    <input type="checkbox" name="selected[]" value="'.$file.'">
                                </form>

                            </span>

then in a seperate PHP file I wrote this algorithm to handle the delete functionality

<?php

foreach($_POST['selected[]'] as $file) {
    if(file_exists($lister->getDirectoryPath() . $file)) {
        unlink($lister->getDirectoryPath() . $file);
        echo "<script type='text/javascript'>alert('Files deleted 
successfully.');</script>";
    }
    elseif(is_dir($file)) {
        rmdir($file);
        echo "<script type='text/javascript'>alert('Files deleted successfully.');</script>";
    }
}

?>

I am pretty sure that I am not performing the handling between the several different types of scripts correctly (specifically having the javascript send its information to the php), however I need some pointers as to where exactly I am failing at this in order to correct it. If needed, I can share more of my source code as it is based on the PHP DirectoryLister found here: https://github.com/DirectoryLister/DirectoryLister

Thanks!

  • 写回答

1条回答 默认 最新

  • dqvs45976 2017-03-31 21:06
    关注

    First, make sure that you're sending the right data to your PHP script. Currently, on click you're calling the function 'rec' and passing it the string 'deletestuff'. You're then passing that string to your script and trying to reference the list of selected checkboxes in your script which you never passed.

    As for passing the list of selected checkboxes to your script, there are a number of ways to do that. Since you're already using jQuery, I would recommend iterating through all the checkbox elements

    $('input[type=checkbox]').each(function () {
        if (this.checked) {
           toDelete.push(this.id);
       }
    });
    

    seeing if they have the attribute checked equal to true, building a list of 'toDelete' from those, and then passing that list to the script through your function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来