javascript - JSON parse not getting value from array -



javascript - JSON parse not getting value from array -

i'm executing json parse on array returned php function , doesn't seem working.

here's php function:

class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html> <head> </head> <body> <?php $bname = $_request["bname"]; $link = mysqli_connect('localhost', 'root', '123'); $servername = "localhost"; $username = "root"; $password = "123"; $dbname = "success"; // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } // php execution $sql = "select id, bname, bicon, rafrica, rasia, roceania, reurope, rsouthamerica, rnorthamerica, traffic, revenue, turn a profit business limit 1"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output info of each row while($row = $result->fetch_assoc()) { $b3name = $row["bname"]. "<br>"; $b3icon = $row["bicon"]. ""; $b3rafrica = $row["rafrica"]. "<br>"; $b3rasia = $row["rasia"]. "<br>"; $b3roceania = $row["roceania"]. "<br>"; $b3reurope = $row["reurope"]. "<br>"; $b3rsouthamerica = $row["rsouthamerica"]. "<br>"; $b3rnorthamerica = $row["rnorthamerica"]. "<br>"; $b3traffic = $row["traffic"]. "<br>"; $b3revenue = $row["revenue"]. "<br>"; $b3profit = $row["profit"]. "<br>"; } } else { echo "0 results"; } $output = array( 'name' => $b3name, 'icon' => $b3icon, 'traffic' => $b3traffic ); echo json_encode($output); ?> </body> </html>

here's ajax contains json parse:

class="snippet-code-html lang-html prettyprint-override"> function loadfacebook1() { var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("b1").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","getfacebook.php",true); xmlhttp.send(); var obj = json.parse(xmlhttp.responsetext); document.getelementbyid("demo").innerhtml=obj.name + "<br>"; }

i'm using

class="snippet-code-html lang-html prettyprint-override"><span id="demo">

to display returned value need assign obj.name (and other elements of array) variable(s) can utilize update other things in page. help appreciated.

cheers,

will

you should move parsing of received json function called when ajax response delivered (onreadystatechange)

javascript php ajax json

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -