2016-03-01 71 views
1

我是从index.php中调用uberGallery(www.uberGallery.net),像这样的工作:结果不与AJAX

<?php 

include_once 'resources/UberGallery.php'; 
include_once 'resources/uberCall.php'; 
?> 
<!DOCTYPE html> 
<html lang="de"> 
    <head> 
     <meta charset="UTF-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     ... 

     <link rel="stylesheet" type="text/css" href="resources/themes/uber-naked/style.css" /> 
     <link rel="stylesheet" type="text/css" href="resources/colorbox/5/colorbox.css" /> 

     <script src="//code.jquery.com/jquery.js"></script> 
     <script type="text/javascript" src="resources/colorbox/jquery.colorbox.js"></script> 

    </head> 
    <body> 
     <div class = "row"> 
      <div class = "col col-lg-12"> 
       <h2 id = "locName"></h2> 
      </div> 
     </div> 

     <div id="gallery"> 
      <?php 
      $path = "london"; 
      getGallery($path); 

      $path = "paris"; 
      getGallery($path); 

      $path = "rome"; 
      getGallery($path); 
      ?> 
     </div> 

     <script type="text/javascript" src='js/UX.js'></script> 

    </body> 
</html> 

的uberCall.php就是这样:

<?php 
include_once ('UberGallery.php'); 

function getGallery($pathToImage){ 
     UberGallery::init() -> createGallery('locations/'.$pathToImage.'/gallery'); 
    } 

if(isset($_POST['image-path'])) { 

    $path = $_POST['image-path']; 

    UberGallery::init() -> createGallery('locations/'.$path.'/gallery'); 

} 

?> 

到目前为止,这是像预期的那样工作。 现在我想从下拉菜单中加载画廊,并只显示选定的画廊。下拉选择在jQuery脚本来完成:

$(document).ready(function() { 

    $("a[rel='colorbox']").colorbox({ 
     maxWidth : "90%", 
     maxHeight : "90%", 
     opacity : ".5" 
    }); 

    $(".dropdown-toggle").dropdown(); 

    $('#demolist li a').click(function(e) { 
     $('#locationSelector').val($(this).text()); 
     var $this = $(this); 
     var selLocID = $this.attr("value"); 

     e.preventDefault(); 

     var dataString = { 
      'location_id' : selLocID 
     }; 

     $.ajax({ 
      type : 'POST', 
      dataType : 'json', 
      url : 'includes/locationAPI.php', 
      data : dataString, 
      success : function(data) { 
       $('#locData').html(''); 
       // erase content if any 

       $('#locName').html(data['name']); 
       // from MySQL 
       // set location headline 

       $.ajax({ 
        type : "POST", 
        data : { 
         'image-path' : data['pfad'] // from MySQL 
        }, 
        url : 'resources/uberCall.php', 
        dataType : "html", 
        async : false, 
        success : function(data) { 
         result = data; 

         alert(result); 
         $('#gallery').html(result); 
        } 
       }); 

      } 
     }); 
    }); 

}); 

如果走这条路的ubergallery没有找到任何文件夹,虽然我adressing完全相同的uber.php因为我从的index.php会。 我已经用xdebug调查了ubergallery.php,它将被调用完全相同的参数。

此外,我尝试在没有init ::的情况下调用createGallery(),并在调用createGallery之前显式创建UberGallery类的新实例。

所有没有运气。

我在这里做错了什么?

+1

我认为路径'locations /'.$ pathToImage。'/ gallery'存在一个问题,因为它是一个相对路径,因此当你调用index.php或resources/uberCall时,它是不同的。 php –

+0

感谢mapek,这正是问题所在。 –

回答

0

在您的 resources/userCall.php脚本中,您实际上没有调用图库函数来渲染它。这似乎是第二个AJAX请求的目标。该脚本仅仅定义了该功能。

编辑:您已经更新了您的问题中的代码以包含该呼叫。

假设image-path是你要提供为$pathToImage,你需要有一个调用从$_POST['image-path']传递值的函数getGallery脚本参数。

例如:

<?php 
require_once('resources/uberCall.php'); 

getGallery($_POST['image-path']); 

注:没有经过任何过滤的禁制,牢记这可能是潜在的危险我能传递一个image-path参数挑选一个顽皮的目录。

然后使用该脚本作为第二个AJAX调用的目标。

P.S.在第二次AJAX调用中删除async: false,它会在请求处于运行状态时停止页面上的任何其他交互操作。

请记住,使用resources/userCall.php中的参数检查,原始请求会从第二个请求中关闭目录。这意味着第二个请求的工作目录将不同,路径将解析为"resources/locations/$path/gallery"而不是"locations/$path/gallery"

您最好在与原始文件相同的目录中创建AJAX操作的第二个脚本,并根据第一个脚本在resource/userCall.php中要求。或使用可配置的绝对目录路径来解决图库位置:

<?php 
require_once('UberGallery.php'); 
require_once('config.php'); 

function getGallery($path) { 
    UberGallery::init() -> createGallery("{$config['gallery_base_path']}/$path/gallery"); 
} 

随着$config['gallery_base_path']是像/absolute/path/to/my/application/locations

不知道文件操作是否会使用包含路径会导致额外的混淆,有些文件选项可能会被告知使用包含当前文件路径和当前脚本目录的include路径,但如果不是,它将只会尝试从当前工作目录中找到该文件。而includerequire都使用包含路径和当前脚本目录作为查找脚本的潜在点。

P.S.P.S使用getcwd可以找出当前工作目录在脚本中的任何位置。

+0

非常感谢Stuart的详细解释。我通过在根目录中推入uberCall.php来解决它,因此它现在在'locations/$ path/gallery'中搜索。正如你所建议的,它正在寻找不存在的'resources/locations/$ path/gallery'文件夹。 –