Tuesday 15 June 2010

javascript - Why is my code not passing to my div? -



javascript - Why is my code not passing to my div? -

i'm trying create gameserver query website, , want load content, save it, , echo later. however, doesn't seem echoing. selects element id , supposed echo content of var.

here's html code:

<center><div id="cstrike-map"><i class="fa fa-refresh fa-spin"></i>&nbsp;&nbsp;<b>please wait ...</b><br /></div>

javascript:

<script type="text/javascript"> var map = ""; var hostname = ""; var game = ""; var players = ""; $.post( "serverstats-cstrike/cstrike.php", { func: "getstats" }, function( info ) { map = ( data.map ); hostname = ( data.hostname ); game = ( data.game ); players = ( data.players ); }, "json"); function echomap(){ document.getelementbyid("cstrike-map"); document.write("<h5>map: " + map + "</h5>"); } </script>

php files: query.php

/* source engine query function, requires server ip:port */ function source_query($ip) { $cut = explode(":", $ip); $hl2_address = $cut[0]; $hl2_port = $cut[1]; $hl2_command = "\377\377\377\377tsource engine query\0"; $hl2_socket = fsockopen("udp://".$hl2_address, $hl2_port, $errno, $errstr,3); fwrite($hl2_socket, $hl2_command); $junkhead = fread($hl2_socket,4); $checkstatus = socket_get_status($hl2_socket); if($checkstatus["unread_bytes"] == 0) { homecoming 0; } $do = 1; while($do) { $str = fread($hl2_socket,1); $hl2_stats.= $str; $status = socket_get_status($hl2_socket); if($status["unread_bytes"] == 0) { $do = 0; } } fclose($hl2_socket); $x = 0; while ($x <= strlen($hl2_stats)) { $x++; $result.= substr($hl2_stats, $x, 1); } $result = urlencode($result); // output homecoming $result; } /* format source engine query (assumes query's results urlencode()'ed!) */ function format_source_query($string) { $string = str_replace('%07','',$string); $string = str_replace("%00","|||",$string); $sinfo = urldecode($string); $sinfo = explode('|||',$sinfo); $info['hostname'] = $sinfo[0]; $info['map'] = $sinfo[1]; $info['game'] = $sinfo[2]; if ($info['game'] == 'garrysmod') { $info['game'] = "garry's mod"; } elseif ($info['game'] == 'cstrike') { $info['game'] = "counter-strike: source"; } elseif ($info['game'] == 'dod') { $info['game'] = "day of defeat: source"; } elseif ($info['game'] == 'tf') { $info['game'] = "team fortress 2"; } $info['gamemode'] = $sinfo[3]; homecoming $info; }

cstrike.php

include('query.php'); $ip = 'play1.darkvoidsclan.com:27015'; $query = source_query($ip); // $ip must contain ip:port $q = format_source_query($query); $host = "<h5>hostname: ".$q['hostname']."</h5>"; $map = "<h5>map: ".$q['map']."</h5>"; $game = "<h5>game: ".$q['game']."</h5>"; $players = "unknown"; $stats = json_encode(array( "map" => $map, "game" => $game, "hostname" => $host, "players" => $players ));

you need display response in $.post callback:

$.post( "serverstats-cstrike/cstrike.php", { func: "getstats" }, function( info ) { $("#map").html(data.map); $("#hostname").html(data.hostname); $("#game").html(data.game); $("#players").html(data.players); }, "json");

you haven't shown html, i'm making ids places want each of these things show.

javascript php jquery html

No comments:

Post a Comment