繁体   English   中英

通过AJAX发送PHP变量值

[英]Send a PHP variable value through AJAX

我目前正在使用AJAX来获取服务器上在线的播放器数量。 我使用以下代码在主页上调用ajax函数:

<script>
$(document).ready(function() {
    setInterval(function() {
        $.get("avatarquery.php", function(players) {
            $("#playersPLZ").text(players);
        });
    }, 3000);
});
</script>

在页面avatarquery.php上,我具有以下PHP代码:

<?php
require_once 'checkinfo.php';
echo getPlayersTotal();
?>

最后,我在checkinfo.php页面上使用以下代码ping服务器并将数据发送回前几页:

<?php

function getPlayersTotal() {
$version = 0;
//ini_set("display_errors", 1);
//ini_set("track_errors", 1);
//ini_set("html_errors", 1);
//error_reporting(E_ALL);


$SERVER_IP = "37.187.139.123"; 
$SERVER_PORT = "26618"; 
$QUERY_PORT = "26618";

$HEADS = "3D"; 
$show_max = "unlimited"; 
$SHOW_FAVICON = "on"; 

$TITLE = "My fancy Serverpage";
$TITLE_BLOCK_ONE = "General Information";
$TITLE_BLOCK_TWO = "Players";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$ping = json_decode(file_get_contents('http://api.minetools.eu/ping/' . $SERVER_IP . '/' . $SERVER_PORT . ''), true);
$query = json_decode(file_get_contents('http://api.minetools.eu/query/' . $SERVER_IP . '/' . $QUERY_PORT . ''), true);


if(empty($ping['error'])) { 
$version = $ping['version']['name'];
$online = $ping['players']['online'];
$max = $ping['players']['max'];
$motd = $ping['description'];
$favicon = $ping['favicon'];
}

if(empty($query['error'])) {
$playerlist = $query['Playerlist'];
}

return $version;
}
echo getPlayersTotal();
?>

当前,$ SERVER_IP和$ SERVER_PORT变量直接在checkinfo.php代码中定义。 但是,我想从我的主页发送这些变量的值。 我该怎么做呢?

将它们作为参数添加到函数中,例如

function getPlayersTotal($ip, $port) { ... }
echo getPlayersTotal($_REQUEST['ipcheck'], ...);

或仅将变量定义移动到主文件中:

$SERVER_IP = $_REQUEST['ipcheck']; 
$SERVER_PORT = $_REQUEST['portcheck']; 
require_once 'checkinfo.php';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM