2017-06-17 52 views
0

我試圖用Ajax顯示我的搜索數據,但它不工作時,我檢查我的控制檯沒有錯誤,首先我從我的引用url中獲取錯誤但我已經修復了,但是當我嘗試搜索數據時,但是我搜索的數據根本沒有顯示。這裏是我的代碼阿賈克斯不顯示搜索值從數據庫

<script type="text/javascript"> 
 
    $(document).ready(function(){ 
 
    $('#cari2').click(function(){ 
 
     $('#table_div').text(""); 
 
     $('#info').text(""); 
 
      $.ajax({ 
 
       url: "cari.php", 
 
       type: "get", 
 
       data: {"cari": $("#cari").val()}, 
 
       success: function(data){ 
 
       $('table_div').html(data); 
 
       }, 
 
       error : function(xhr, teksStatus, kesalahan){ 
 
       $('info').html('<br>Error</br>') 
 

 
       } 
 
      }); 
 
    }); 
 

 

 
    }); 
 
    </script>

這一個從我的 「cari.php」:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<link href="../../css/show_sisByName.css" rel="stylesheet" type="text/css" /> 
 
</head> 
 
<body> 
 
<form method="get"> 
 
<table> 
 
    <?php 
 

 
    if(isset($_GET['cari'])){ 
 
    $cari = trim($_GET['cari']); 
 

 
    ?> 
 
    <thead> 
 
    <th>NAMA SISWA</th> 
 
    <th>AKSI</th> 
 
    </thead> 
 
<tbody> 
 
<?php 
 
    include "db_connect.php"; 
 
    try{ 
 
     $kueri = $dt_bas->prepare("SELECT nm_dpn FROM siswa WHERE nm_dpn LIKE :cari"); 
 
     // $kueri->bindParam(1, $cari); 
 
     $kueri->execute(array(':cari' => '%'.$cari.'%')); 
 
    }catch(PDOException $e){ 
 

 
    } 
 
    while ($row = $kueri->fetch(PDO::FETCH_ASSOC)) { 
 
?> 
 
      <!--- Tabel Row Start ASC------------------> 
 
    <tr> 
 
    <td><?php echo $row["nm_dpn"];?></td> 
 

 
    <td> 
 
     <a href="#">Ubah<a/> 
 
     <a href="#">Detail</a> 
 
    </td> 
 
    </tr> 
 
<?php 
 
    } //end of while cari 
 
}  
 
?> 
 
</tbody> 
 
</table> 
 
</form> 
 
</body> 
 
</html>

感謝的您的時間。

+2

什麼是'table_div'?選擇器'$('table_div')'發現__nothing__,我想它是'$('#table_div')'。 –

回答

1

你的jQuery選擇器是錯誤的。你應該在id的選擇器中使用#。 $('#table_div').html(data);

<script type="text/javascript"> 
    $(document).ready(function(){ 
    $('#cari2').click(function(){ 
     $('#table_div').text(""); 
     $('#info').text(""); 
      $.ajax({ 
       url: "cari.php", 
       type: "get", 
       data: {"cari": $("#cari").val()}, 
       success: function(data){ 
       $('#table_div').html(data); 
       }, 
       error : function(xhr, teksStatus, kesalahan){ 
       $('info').html('<br>Error</br>') 

       } 
      }); 
    }); 


    }); 
    </script>