NineNine
NineNine

Reputation: 324

Getting string from AJAX

I've been searching around on google for a while, trying to find an easy way to make AJAX work. I've found some things but so far I haven't been lucky with getting them to work. I would like to get a string from a PHP file called 'serverstatus.php'. The string is currently outputted on the serverstatus page like this: "Online:0:20 Offline" Explanation: There are two servers, the first one is online has 0 out of 20 clients. The second one is offline.

What i want it to do is load these values via AJAX, then split them with " " then split each of those values i got by ":". That should give me everyhting i want.

The code i have to get the string is this:

<head>
<script>
    var someVar;
    $.ajax({url: $_SERVER['DOCUMENT_ROOT'].'/inc/int/serverstatus.php', success: function(ajaxResponse) {
        someVar = ajaxResponse;
        document.write(someVar);
    }});
</script>
</head>

NOTE: I want the code to be called as soon as the page is starting to load.

EDIT: I think the problem is that it's not getting any values from the status page. Also, the values on the status page are loaded from an MySQL server, so it takes about 1-3 seconds to show up.

Upvotes: 0

Views: 202

Answers (4)

Alex W
Alex W

Reputation: 38173

This should work:

var someVar;
$.ajax({url: $_SERVER['DOCUMENT_ROOT'].'/inc/int/serverstatus.php', success:
function(ajaxResponse)
{
     var array = ajaxResponse.split(" ");
     for(var i in array)
     {
        var subArray = array[i].split(":");
        if(array[i] === "Offline")
            document.body.innerHTML += "Server "+i+" is offline.<br/>";
        else
        {
            document.body.innerHTML += "Server "+i+" has "+subArray[1]+" out of "+subArray[2]+" clients online.<br/>";
        }
     }
}
});

http://jsfiddle.net/VusC8/

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150020

"then split them with " " then split each of those values i got by ":"

So what's the problem? Implementing exactly what you described is simple:

var tmp = ajaxResponse.split(" ")[0].split(":");
var currentClients = tmp[1];
var maxClients = tmp[2];

...though if the string that comes back from the server is in the wrong format you'll obviously have a problem so you may want to do the .split() operations separately.

Or you could just use a regex instead of .split():

var currentClients,
    maxClients,
    matches = ajaxResponse.match(/(\d+):(\d+)/);
if (matches){
   currentClients = matches[1];
   maxClients = matches[2];
} else {
   // response was in the wrong format, so do something else
}

Upvotes: 1

ajtrichards
ajtrichards

Reputation: 30565

This will run as soon as the page has loaded and split your strings. The data will then be put into the div with the ID - response_container

<html>
<head>
<script type="text/javascript">

$(document).ready(function(){

    var someVar;
    $.ajax({
        url: 'serverstatus.php',
        success: function(ajaxResponse) {

            var data = ajaxResponse.split(" ")[0].split(":");
            currentClients = data[1];
            maxClients = data[2];

            $("#response_container").append('Clients: ' + currentClients + ', Max: ' + maxClients);

        }
    });

});
</script>
</head>
<body>
    <div id="response_container"></div>
</body>
</html>

Upvotes: 1

Frank Visaggio
Frank Visaggio

Reputation: 3712

var str="Online:0:20 Offline";
var n=str.split(":");

n will be an array of your values.

see more about the split function here

edit: i think i miss interpreted whats going wrong maybe, can you update your input string and what you want as an output

Upvotes: 0

Related Questions