2015-10-15 71 views
3

我有3個頁面,一個html,javascript和php文件。當在html頁面中按下一個按鈕時,php頁面將在數據庫中搜索包含按鈕名稱作爲關鍵字的行並返回一些行。我想在html頁面中顯示所有這些行,但是當我發回數據時,它不會顯示。這裏是我的三頁代碼。如何通過html標籤顯示多行變量

Html頁面

<button name = 'science'>  Science</button> 
<button name = 'engineering'> Engineering</button> 
<button name = 'math'>   Math</button> 
<button name = 'law'>   Law</button> 
<button name = 'arts'>   Arts</button> 
<button name = 'business'>  Business</button> 
<button name = 'fiction'>  Fiction</button> 
<button name = 'selfhelp'>  Self Help</button> 
<button name = 'labequipment'> Lab Equipment</button> 

<p id = 'feedback'></p> 

<script type ="text/javascript" src ="jquery.js"></script> 
<script type ="text/javascript" src ="catselect.js"></script> 

JavaScript頁面

$(":button").click(function(){ 
    var cat = $(this).attr("name"); 

    $.get("trier.php",{input: cat}, function(data){ 
     $("#feedback").html(data); 
    }); 
}); 

PHP頁面

if(isset($_GET['input'])){ 
    $catt = $_GET['input']; 

     $info = "SELECT * FROM books WHERE (Category = '$catt')"; 

     while(mysqli_query($conn,$info)){ 
      $information = mysqli_query($conn,$info); 
      $intel = mysqli_fetch_assoc($information); 

      echo $intel['Title']; 
      echo nl2br("\n"); 
      echo $intel['Course']; 
     } 
} 

我想顯示多行包括 '標題' 欄和 '課程' columnn在id =「display」的php頁面中的標籤。關於如何修改我的代碼來做到這一點的任何想法?

回答

1

$(":button")應該$("button")

u可以使用表的PHP回聲HTML,但我建議使用JSON或使用angularjs

ID不應該頻繁地改變,id = "display" better use class="display"代替

0

你可以做的簡單方法like @Yanjun Lin建議在你的php上使用html表格 if(isset($ _GET ['input'])){ $ catt = $ _GET ['input'];

 $info = "SELECT * FROM books WHERE (Category = '$catt')"; 
     $html=''; 
     while(mysqli_query($conn,$info)){ 
      $information = mysqli_query($conn,$info); 
      $intel = mysqli_fetch_assoc($information); 
      $html=$html.'<tr>'; 
      //echo $intel['Title']; 
      //echo nl2br("\n"); 
      //echo $intel['Course']; 

      $html=$html.'<td>'.$intel['Title'].'</td>'; 
      $html=$html.'<td>'.$intel['Course'].'</td>'; 
      $html=$html.'</tr>'; 
     } 
     echo $html; 
在你的JavaScript

<button name = 'science'>  Science</button> 
    <button name = 'engineering'> Engineering</button> 
    <button name = 'math'>   Math</button> 
    <button name = 'law'>   Law</button> 
    <button name = 'arts'>   Arts</button> 
    <button name = 'business'>  Business</button> 
    <button name = 'fiction'>  Fiction</button> 
    <button name = 'selfhelp'>  Self Help</button> 
    <button name = 'labequipment'> Lab Equipment</button> 

<table id = 'feedback'></table> 

<script type ="text/javascript" src ="jquery.js"></script> 
<script type ="text/javascript" src ="catselect.js"></script> 

我沒有對你的PHP測試,但不希望它的工作原理:在您的HTML(改變<p>標籤<table>標籤)

$("button").click(function(){ 
var cat = $(this).attr("name"); 

$.get("trier.php",{input: cat}, function(data){ 
    $("#feedback").html(data); 
}); 

...也JSFiddle它只會在javascript和html中看起來像這樣。也請考慮使用json來使用正確的標準,例如@Yanjun Lin建議