2016-03-01 68 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'文件夾。 –