簡體   English   中英

使用AJAX在服務器上執行PHP

[英]Execute PHP on server with AJAX

嗨,我正在嘗試讓ajax在服務器上執行代碼並返回結果:

<script>
    $(document).ready(function(){
        $.post('includes/gallery/gallery.php', function(data){
            $('#galleryTab').html(data);
        });
    });
</script>

class Main{
public static function generateGallery(){
    $path = 'includes/gallery/';
    $results = glob($path . '*' , GLOB_ONLYDIR);
    $dirCount = 0;
    foreach ($results as $result) {
        $resultBaseName = basename($result);
        if ($resultBaseName === '.' or $resultBaseName === '..') continue;
        echo '<h2>'.$resultBaseName.'</h2>';
        if (is_dir($path . '/' . $resultBaseName)) {
            $dir = $path.$resultBaseName.'/';
            $images = glob($dir."*.jpg");
            $imageCount = 0;
            foreach($images as $image) {
                echo '<div class="galleryImage" id="openImage'.$imageCount.$dirCount.'"><img src="'.$image.'" /></div>';
                $imageCount++;
            }
        }
        echo '<br />';
        $dirCount++;
    }
}
}

它沒有在服務器上執行PHP,請幫助

您在此處定義了類及其方法,但並不是根據顯示給我們的代碼來調用它。 從“ main”類中調用“ generateGallery”方法,它應該可以工作。

您提供的代碼還不夠。 您應該從函數返回結果作為數組,並使用json_encode php函數或任何其他交換格式對其進行編碼。 請參閱以下聲明。

            $returnResult = json_encode($array);

然后在客戶端使用$ .ajax({})jquery方法。 請參見下面的示例。

 $.ajax({

      url : url , // <-your url here
      method : post or get ,
      data : { field: value }, //provide the values you are passing  <-
      dataType: json or html,  //data interchange format
      success : function(data){

        console.log(data.value);

       }

      });

在這里查看jquery $ .ajax({})文檔Jquery文檔鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM