سؤال

I have coded a php script that is used to check the status of several different servers, determine if they are online, and then output their version information as well as give the option to update databases on the server.

The problem I'm having is that when the script reaches a server that is offline, it hangs for a very long time while it performs the check. I figured that a good way around this may be to use something like jQuery or XAJAX to load the different servers asynchronously, so that one server doesn't hang the entire script.

I call a single function to begin all of the checks, and echoing of server information:

    outputServerInfo("addressOfServerHere", "nameofServerHere", array("Names", "of", "databases");

My question is: what would be the best method to load these servers asynchronously? If I could get it to say "Loading..." in the place of each server while it performs the checks, that would be ideal. I haven't used jQuery or XAJAX before, so I thought it best to get some input the community. Thanks!

EDIT: I've now got it working and loading all of my servers, but I've got another problem.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">

document.write("<h1>Server Manager</h1><form method='post' name='form1'>");

function server_output(serverName) {
        document.write("<div id=" + serverName + "Loading>Loading " + serverName + "...</div>");
        $.ajax({
        url  : 'serverManager.php',
        async: true,
        type : 'POST',
        data : 'serverName=' + serverName,
        success : function(msg){
            document.write("<div id=" + serverName + "Loading>");
            document.write(msg);
            document.write("</div>");
        }
    });
}
server_output("serverName");
</script>
<input type='SUBMIT' value='GO' name='btnGo' onSubmit=document.form1.submit(); />
<input type='HIDDEN' name='ACTION' value='GO' />

When the I load the page, I can see the Server Manager header, as well as the button that comes after the last tag, however, they disappear almost immediately. The server information loads after that and they don't reappear, so I'm left without my title as well as the button that lets me use my form(my php script creates checkboxes that are used to submit jobs to the servers for updating). Any ideas?

هل كانت مفيدة؟

المحلول

Well for jQuery it's as easy as calling your backend PHP script.

$.ajax({
    type : 'POST',
    url : 'your_script.php',
    async : true, //default anyway
    data : '', //server ip, or something to send to the script using type as the method
    success : function(msg){
        //do something here with returned data
        //Example: alert(msg) will show the output results from the backend script
    }
});

نصائح أخرى

You're using AJAX to POST, but you're checking $_GET - think you might want $_POST.

You could make all your calls from client-side with the jquery AJAX function. Thus, you can make all your calls asynchronously.

jQuery and AJAX is definitely the way to go. jQuery AJAX is asynchronous, so you can kick off a lot of jobs at one time. You would kick off an AJAX job for each server that you are trying to get updates for.

By default, you could have a div for each server with the content of "Loading." Once you get a successful response from the AJAX call, you would change the value of the div to the information that you would like to display. Even a failure will return a result from AJAX, so you could trap the error and display a different message.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top