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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料