2017-05-30 3 views
-3

jsonからデータhtmlを表示することは可能ですか? 私は例を持っていますが、テーブルのためだけです。データjsonをhtml(テーブルではない)に保存することが可能です

私はJSONからHTMLに/負荷データを表示したい
<table id="tables" 
data-url="comments.php" 
data-row-style="rowStyle" 
data-toggle="table" 
data-show-refresh="true" 
data-show-toggle="true" 
data-show-columns="true" 
data-search="true" 
data-select-item-name="toolbar1" 
data-pagination="true" 
data-sort-name="name" 
data-sort-order="desc"> 
<thead> 
<tr> 
<th data-field="name" data-sortable="true" data-align="left">Name</th> 
<th data-field="comments" data-sortable="true" data-align="left">comments</th> 
</tr> 
</thead> 
<tr> 
</tr> 
</table> 

<h3>name</h3>//data from json 
<p>comments</p>//data from json 

comments.php

<?php 
header('content-type:application/json'); 
include 'connection.php'; 

$select = mysql_query("select * from comments"); 
$row=array(); 

while($row=mysql_fetch_array($select)) 
{ 
    $arrayx=array( "comments"=>$row['comments'], 
        "name"=>$row['name']); 

    $rows[] = $arrayx; 

} 
echo json_encode($rows); 
?> 

私はそのような例のJSONを持っている...

+0

ええ、AJAXを使用してください。 –

+0

* "私は例がありますが、テーブルのためだけです" * - あなたの例はHTMLだけですが、JSONとの接続はまったくありません。 JSONからその表を移入するために使用する対応するJSは何ですか? – nnnnnn

+0

私はjsonComments.phpを追加しました –

答えて

0

それができます尋ねると、あなたのHTMLのインラインになることはありません。

しかし、jQueryの.get()を使用して、それがこの方法を行うことができます。

HTML:

<h3 id="jsonName"></h3><!--data from json will be here --> 
<p id="jsonComments"></p><!--data from json will be here --> 

jQueryのスクリプト:

$.get("comments.php","",function(data){ 
    $("#jsonName").html(data.name); 
    $("#jsonComments").html(data.comments); 

    //To look at the received json 
    console.log(JSON.stringify(data)); 
},"json"); 

あなたのJSONは多くのオブジェクト...使用のクラスを持っている場合idsの代わりに。
データをループスルーします。

+0

は働いていません。 jsonはエンコードする必要がありますか? –

+0

'comments.php'が有効なjsonを返すと仮定しました。 –

+0

編集しました...試してみて、コンソールに表示された内容を投稿してください。 –

関連する問題