2016-03-02 28 views
0

我有一個表單來搜索數據庫中的任何人。 然後我想在DataTable中顯示搜索結果。AJAX&PHP:從表單提交GET請求的DataTable顯示搜索結果

我使用刀片模板化HTML。

這是我的形式:

<form id="myform" method="GET" action="{{ site_url('person/search') }}"> 

這是我的DataTable節在我的HTML

<section class="panel" style="margin-top:20px;"> 
     @include(config_item('theme').'.person.part.table-person', compact('result'), ['id' => 'persons']) 
</section> 

這是我的表:

<table id="{{ $id }}" data-toggle="table" data-mobile-responsive="true"> 
    <thead> 
     <tr> 
      <th data-field="image" data-align="center">Image</th> 
      <th data-field="id" data-visible="false">ID</th> 
     </tr> 
    </thead> 
</table> 

而且最後是我的AJAX代碼:

$(document).ready(function() { 
     $('#myform').submit(function() { 
      $.ajax({ 
       dataType: json, 
       data: $(this).serialize(), 
       type: $(this).attr('method'), 
       url: $(this).attr('action'), 
       success: function(response) { 
        $('#persons').html(response); 
       } 
      }); 
      return false; 
     }); 
    }); 

使用所有代碼,我無法顯示搜索結果。

我做錯了什麼? 感謝

+0

什麼是響應返回的數據? – aldrin27

+0

'dataType:引號之間的「JSON」 –

回答

0

確保..你不能重複相同的ID名稱 使用JQ追加顯示錶詳細

$('#channels').append(' <tr> 
     <th data-field="image" data-align="center">Image</th> 
     <th data-field="id" data-visible="false">ID</th> 
    </tr>'); 

提示:http://api.jquery.com/append/