Thursday, 15 April 2010

display php data with jQuery/AJAX -



display php data with jQuery/AJAX -

for project form school want build live score on site. live score has display surname (voornaam) lastly name (achternaam) , score of player.

i made php file (feed.php) executes sql query select 10 values database (2 tables combined) feed.php encode , array (json_encode). working , when print result.

but have create phone call index.html feed.php jquery/ajax. want refresh score every sec see if got improve score.

i managed displayed i've see "undefined" , keeps loading. after min got 60 "scores".

what doing wrong , how can show 10 records on page?

so much in advance!

this code use:

index.html:

<script> $.get("feed.php", function(data) { $("#score") .append("<tr><td>" + data.voornaam + "</td><td>" + data.achternaam + "</td><td>" + data.score +"</td></tr>").fadein("slow"); }, "json") </script>

feed.php:

$sql = "select game.userid, game.score, roboshooter.id, roboshooter.voornaam, roboshooter.achternaam game, roboshooter game.userid = roboshooter.id order game.score desc limit 10 "; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_assoc($result)) { $return = json_encode(array( "voornaam" => $row['voornaam'], "achternaam" => $row['achternaam'], "score" => $row['score'] )); print_r($return); }

jules

the print_r renders json response invalid.

$return = array(); while($row = mysqli_fetch_assoc($result)) { $return[] = $row; } echo json_encode($return);

you must homecoming single json object, otherwise it's invalid.

since returns array you'll have adapt javascript well:

for (var = 0; < data.length; i++) { $("#score").append("<tr><td>" + data[i].voornaam + "</td><td>" + data[i].achternaam + "</td><td>" + data[i].score +"</td></tr>").fadein("slow"); }

php jquery html ajax

No comments:

Post a Comment