为什么我的代码没有传递给我的div?

时间:2014-11-08 22:32:21

标签: javascript php jquery html

我正在尝试为我的网站创建一个游戏服务器查询,我想让它加载内容,保存并稍后回复。然而,它似乎并没有回应。它按ID选择元素,并且应该回显VAR的内容。

这是我的HTML代码:

<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( data ) {
            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文件:


query.php

/* SOURCE ENGINE QUERY FUNCTION, requires the 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)
{
    return 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); // the output
return $result;
} 

/* FORMAT SOURCE ENGINE QUERY (assumes the query's results were 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];
return $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
 ));

3 个答案:

答案 0 :(得分:0)

我可以从你的代码中理解一些事情,并且echoMap()有点混乱......但是假设你的php没问题,那么你似乎没有调用echomap函数邮政请求已完成。

echoMap()

之后立即添加 players = ( data.players );

如果要修改的div ID是&#39; cstrike-map&#39;你可以使用jQuery:

将JS echoMap 更改为此

function echoMap(){
    $("#cstrike-map").html("<h5>Map: " + map + "</h5>");
}

答案 1 :(得分:0)

您需要在$.post回调中显示回复:

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

您还没有显示您的HTML,因此我只是为您希望展示这些内容的地方编制ID。

答案 2 :(得分:0)

所以我做的是我必须将我需要的内容回显到PHP文件中,然后获取HTML内容并使用它。

这似乎是我在OP中做我想做的最强大,最简单的方式。

<script type="text/javascript">
            $(document).ready(function(){
                $.post("stats/query.cstrike.php", {}, 
                    function (data) {
                        $('#serverstats-wrapper-cstrike').html (data);
                        $('#serverstats-loading-cstrike').hide();
                        $('#serverstats-wrapper-cstrike').show ("slow");
                    });
            });
        </script>

<强> PHP

<?php
    include 'query.php';
    $query = new query;
    $address = "play1.darkvoidsclan.com";
    $port = 27015;
    if(fsockopen($address, $port, $num, $error, 5)) {          
        $server = $query->query_source($address . ":" . $port);
        echo '<strong><h4 style="color:green">Server is online.</h4></strong>';
        if ($server['vac'] = 1){
            $server['vac'] = '<img src="../../images/famfamfam/icons/tick.png">';
        } else {
            $server['vac'] = '<img src="../../images/famfamfam/icons/cross.png">';
        }
        echo '<b>Map: </b>'.$server['map'].'<br />';
        echo '<b>Players: </b>'.$server['players'].'/'.$server['playersmax'].' with '.$server['bots'].' bot(s)<br />';
        echo '<b>VAC Secure: </b>&nbsp;'.$server['vac'].'<br />';
        echo '<br />';
    } else {
        echo '<strong><h4 style="color:red">Server is offline.</h4></strong>';
        die();
    }
?>