如何通过html标记显示多行变量

时间:2015-10-15 01:46:02

标签: javascript php jquery html

我有3个页面,一个html,javascript和php文件。当在html页面中按下一个按钮时,php页面在数据库中搜索按钮的名称为键的行,并带回多行。我想在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'];
        }
}

我想显示很多行,包括&#39;标题&#39;专栏和课程&#39;在php页面的标签中的columnn,id =&#34; display&#34;。关于如何修改我的代码来做任何想法?

2 个答案:

答案 0 :(得分:1)

$(":button")应为$("button")

你可以使用表格的php echo html,但我建议使用json或使用angularjs

ID不应经常更改,id = "display" better use class="display"而不是

答案 1 :(得分:0)

你可以像@Yanjun Lin建议使用html表那样简单 在你的PHP上:     如果(isset($ _ GET [ '输入'])){         $ 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").click(function(){
var cat = $(this).attr("name");

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

(将<p>标记更改为<table>代码):

<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,但希望它有效......还JSFiddle它只会在javascript和html中看起来像这样。也可以考虑使用json作为正确的标准,如@Yanjun Lin建议