Sunday 15 January 2012

php - How to display JSON information using AJAX -



php - How to display JSON information using AJAX -

i creating file displays json file using ajax. problem have right prints first row, want print 4 rows. tried adding print statement under original 1 in json.php caused errors.

here function json in index.html file

function getjson() { var xmlhttp = xmlhttpobjcreate(); if (!xmlhttp) { alert("the browser doesn't back upwards action."); return; } xmlhttp.onload = function() { if (xmlhttp.status == 200) { // response text var response = xmlhttp.responsetext; // prints json string console.dir(response); // div object var divobj = document.getelementbyid('dinojson'); // used json.parse turn json string object var responseobject = json.parse(response); // our object console.dir(responseobject); // can utilize object so: divobj.innerhtml = responseobject.name + " lived during " + responseobject.pet; } } xmlhttp.open("get", "json.php", true); xmlhttp.send(); }

json.php

<?php // print json $result = array('name' => 'staurikosaurus', 'pet' => 'triassic'); $result2 = array('name' => 'diplodocus', 'pet' => 'jurassic'); $result3 = array('name' => 'stegosaurus', 'pet' => 'jurassic'); $result4 = array('name' => 'tyrannosaurus', 'pet' => 'cretaceous'); print json_encode($result); ?>

ajax.js

function xmlhttpobjcreate() { var xmlhttp; seek { xmlhttp=new xmlhttprequest(); } grab (e) { seek { xmlhttp=new activexobject("msxml2.xmlhttp"); } grab (e) { seek { xmlhttp=new activexobject("microsoft.xmlhttp"); } grab (e) { homecoming false; } } } homecoming xmlhttp; }

so right when button pressed programme displays this:

staurikosaurus lived during triassic

and want programme display this

staurikosaurus lived during triassic diplodocus lived during jurassic stegosaurus lived during jurassic tyrannosaurus lived during cretaceous

my bad deleted other reply seek changing json array this.

$result = array( array( "name" => "staurikosaurus", "pet" => "triassic" ), array( "name" => "diplodocus", "pet'" => "jurassic" ), array( "name" => "stegosaurus", "pet'" => "jurassic" ), array( "name" => "tyrannosaurus", "pet'" => "cretaceous" ) ); print json_encode($result);

php jquery html ajax json

No comments:

Post a Comment