I would like to retrieve each element inside of this.responseText
, and put them in my HTML on Javascript. Is there something wrong in my code? I hope my code helps you to understand my question. Thanks.
(p.s. the html code is provided, and so I cannot use jquery.)
an example of this.responseText
is below; (By using alert
, I got that)
{"name":"Hermione Grainger","number":"4","review":"Not as good as the NEXT book in the series, but hilarious and satisfying."}{"name":"Ronald Drumpf","number":"1","review":"Feminist propaganda!"}{"name":"Brienne of Tarth","number":"5","review":"Alanna is my best friend."}
And my java script is below;
function bookReview(){
var reviews = JSON.parse(this.responseText);
for(var i=0; i<reviews.length; i++){
var name = document.createElement("h3");
var text = document.createElement("p");
name.innerHTML = reviews[i].name + reviews[i].number;
text.innerHTML = reviews[i].review;
document.getElementById("reviews").appendChild(name);
document.getElementById("reviews").appendChild(text);
}
}
or is there something wrong in my PHP code??
$path = "books/$book/";
review(glob($path . "review" . "*" . ".txt"));
function review($reviews) {
foreach ($reviews as $each) {
$review = file($each, FILE_IGNORE_NEW_LINES);
$output = array (
"name" => $review[0],
"number" => $review[1],
"review" => $review[2]
);
header("Content-type: application/json");
print(json_encode($output));
}
}