weixin_33690963 2019-02-11 08:18 采纳率: 0%
浏览 36

如何创建ajax导航

For a filemanagement, i am using the var $dir that shows me the path to the folders. My url string looks like this: www.example.com/index.php?dir=uploads/folder1

Below is how i grab the path for navigation:

$MainFolderName = 'uploads';
$dir = $MainFolderName;

/* Read actual dir from url */
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$strArr = explode("=",$actual_link);
$CurrentPath = $strArr[1];

// if new folder is created
if(isset($_GET['dir'])) {
$dir = $CurrentPath;
}

To navigate, i use anchors like below:

foreach ($files as $file) {
      .........

if(is_dir($dir.'/'.$file)) {

    $folderanchor = "<a class='folderanchor' href='?dir=".$dir.'/'.$file."'><i class='fas fa-folder'></i>$file</a>";

    echo $folderanchor;

So navigate like this gives always a complete page loading. How can i make this navigation with ajax, so loading dynamically? I know it has something to do with the GET but i dont know how to handle with this...

By the way: for reading all files and directories in a folder is use

// exclude dot, double dot and tmp folder
$files = array_diff( scandir($dir), array(".", "..", "tmp") );
  • 写回答

2条回答 默认 最新

  • George_Fal 2019-02-11 08:51
    关注

    Maybe on page one you could add the following script

    <script type="text/javascript">
    //on page 1.php
    $("body").on('click','a', function(event) {
    event.preventDefault();
    //Here add script to catch the url
    //catching the text
    var link= $(this).text;
    $.post('anotherpage.php', {url:url}, function(data, textStatus, xhr) {
    //Hide the other  div container
    $("#con").remove();
    //append the new response
    $("body").append(data);
    });
    });
    </script>
    

    on anotherpage.php you should put

    if(isset($_GET['dir'])) {
    $dir = $CurrentPath;
    foreach ($files as $file) {
            .........
    
            if(is_dir($dir.'/'.$file)) {
    
            $folderanchor = "<a class='folderanchor' href='?dir=".$dir.'/'.$file."'><i class='fas fa-folder'></i>$file</a>";
    
            echo $folderanchor;
    }
    

    You might run into errors but i tried to give a start..... cheers

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog