2017-08-16 77 views
2

夥計們,請看看什麼是錯在我的代碼基本上是由AJAX後調用。相同的記錄顯示在其他頁面,但我單擊load more button get_data.php似乎不從數據庫獲取記錄,並Iinstead獲取空白頁。Ajax調用PHP腳本沒有錯誤不會記錄

我曾嘗試回聲$貓,$行負載點擊更多的時候,他們顯示的index.php記錄。

<?php 
require('includes/config.php'); 
$cat = 2; 
$row = 2; 

$rowperpage = 2; 

echo $sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage"); 
$sth->bindParam(':catID', $cat, PDO::PARAM_INT); 
$sth->bindParam(':row', $row, PDO::PARAM_INT); 
$sth->bindParam(':rowperpage', $rowperpage, PDO::PARAM_INT); 
$sth->execute(); 

$results = $sth->fetchall(PDO::FETCH_ASSOC); 

$html = ''; 

$html .= '<div class="row">'; 
$html .= '<div class=" col-sm-12 col-md-6 col-lg-6">'; 
$html .= '<div class="row">'; 
$html .= ' <div class="leftbar_content">'; 

foreach ($results as $rows) { 
    $id = $rows['postID']; 
    $img = $rows['postImg']; 
      if(!empty($img)){ 
      $img ="<img src='".DIR."uploads/images/".$img."' alt=''"; 
      }else{ 
      $img = ""; 
      } 
      $youtube = $rows['postYoutube']; 
      if(!empty($youtube)){ 
      $youtube ='<div class="videoholder"> <iframe width="720" height="450" src="//www.youtube.com/embed/'.$youtube.'?autohide=1&fs=1&autoplay=0&iv_load_policy=3&rel=0&modestbranding=1&showinfo=0&controls=1&hd=1" frameborder="0" allowfullscreen=""></iframe></div>'; 
      }else{ 
      $youtube = ""; 
}     
       $html .= '<div id="post_'.$id.'">'; 

      $html .= '<div class="single_stuff wow fadeInDown">'; 
       $html .= '<div class="single_stuff_img"> <a href="'.DIR.'post/'.$rows['postSlug'].'">'.$img.$youtube.'</a> </div>'; 
       $html .= '<div class="single_stuff_article">'; 
       $html .= '<div class="single_sarticle_inner"> <a class="stuff_category" href="'.DIR.'category/'.getCatUrl($rows['catID']).'">'.getCatName($rows['catID']).'</a>'; 
        $html .= '<div class="stuff_article_inner"> <span class="stuff_date">'.date('jS M Y', strtotime($rows['postDate'])).'</span>'; 
        $html .= '<h2><a href="'.DIR.'post/'.$rows['postSlug'].'">'.$rows['postTitle'].'</a></h2>'; 
        $html .= '<p>'.$rows['postDesc'].'</p>'; 
       $html .= '</div>'; 
       $html .= '</div>'; 
       $html .= '</div>'; 
      $html .= '</div>'; 

      $html .= '</div>'; 

} 

echo $html; 

,這裏是我的js

<script type="text/javascript"> 
$(document).ready(function(){ 
    // Load more data 
    $('.load-more').click(function(){ 
     var row = Number($('#row').val()); 
     var catid = Number($('#catid').val()); 
     var allcount = Number($('#all').val()); 
     row = row + 2; 
     if(row <= allcount){ 
      $("#row").val(row); 
      $.ajax({ 
       url: 'get_data.php', 
       type: 'post', 
       data: {row:row,catid:catid}, 
       beforeSend:function(){ 
        $(".load-more").text("Loading..."); 
       }, 
       success: function(response){ 
        // Setting little delay while displaying new content 
        setTimeout(function() { 
         // appending posts after last post with class="post" 
         $(".post:last").after(response).show().fadeIn("slow"); 
         var rowno = row + 4; 
         // checking row value is greater than allcount or not 
         if(rowno > allcount){ 

          // Change the text and background 
          $(".load-more").text("Sorry , No more posts to load"); 
          $('.load-more').css("background","#FB0925"); 
         }else{ 
          $(".load-more").text("Load more"); 
         } 
        }, 2000); 
       } 
      }); 
     }else{ 
      $('.load-more').text("Loading..."); 
      // Setting little delay while removing contents 
      setTimeout(function() { 
       // When row is greater than allcount then remove all class='post' element after 3 element 
       $('.post:nth-child(3)').nextAll('.post').remove().fadeIn("slow"); 
       // Reset the value of row 
       $("#row").val(0); 
       // Change the text and background 
       $('.load-more').text("Load more"); 
       $('.load-more').css("background","#333333"); 
      }, 2000); 
     } 
    }); 
}); 
</script> 

共享代碼,希望我得到了一些解決方案。

謝謝

+1

請發表您的javascript函數。 –

+0

@TrươngCôngHậu我剛剛更新了js –

+0

1- $ results = $ sth-> fetchall(PDO :: FETCH_ASSOC);在ajax函數中打印這個結果2-alter response可能是你的問題。 – Kamal

回答

0

也許這會有所幫助。

通常與LIMIT子句當在仿真模式(它是在默認情況下),PDO替換佔位符與實際數據時,而不是單獨發送。

嘗試打開仿真關閉。

由:$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

然後改變這:

echo $sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage"); 

$sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage"); 
+0

收到這個錯誤致命錯誤:未捕獲的異常「PDOException」與消息「SQLSTATE [HY093]:無效參數數:參數沒有被定義的」使用像這樣我得到表示$記錄結果= $ DB->查詢('SELECT * –

+0

FROM blog_posts WHERE catID ='。$ cat。'ORDER BY postID DESC LIMIT'。$ row。','。$ records_per_page。'') - > fetchAll(); –

+0

但我需要參數在BindParam,但它只是不工作,如果我綁定它們 –