2017-01-12 10 views
0

私はトピックの位置とトピックをgetTopics()関数で返そうとしています。たとえば、 "楽器"を返すと整数0が返され、getFaqs()関数で使用できます。 "プログラミング言語"を返すと、整数1なども返されます。私はパラメータtopic_id(整数)とトピック(テキスト)を含めたので、なぜこの関数が動作しないのか分かりません。php配列のアイテムの位置整数をPHPからHTMLにするには?

のindex.html:

function getTopics() { 
    $.getJSON("topics.php", function(result) { 
     $("#topic-container").html(""); 
     $.each(result, function(topic_id, topic){ 
      $("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos 
     }); 
    }); 
    return false; 
} 

topics.php:

<?php 

header('Content-Type: application/json; charset=utf-8'); 
$topics = array("Musical instruments", "Programming languages", "Varieties of pizza"); 
?> 
+0

あなたは魔法のように何を期待していますそのPHPで起こる?たぶん 'echo json_encode($ topics);' – AbraCadaver

答えて

1

だけアレイ上standard for loopを使用します。

var topic, topic_id; 
for (topic_id = 0; topic_id < result.length; topic_id++) { 
    topic = result[topic_id]; 
    $("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos 
} 
関連する問題