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

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

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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?