This first example takes three values entered into a form and populates the fourth field with data from the server.

Enter two numbers, select the operation you want, and then click or tab into the answer field. Try changing your choices and clicking Calc. Subsequent remote procedure calls are generally faster than the first. Although, that depends on browser settings.

Arithmetic     =

The operands and operation choice are sent to the server. PHP script on the server performs the calculation and returns the answer.

Not real exciting, huh. This could have been done on the client, but imagine, if you will, a more complicated calculation. One that uses tables to lookup values as well as arithmetic. This could easily be an amortization or annuity calculation. The formula could be proprietary or one you don't want visible to the world in downloaded script.

The functions do_rpc and createXMLHttp and handleResponse used in this example are exactly as shown in the preceding sections. responseText contains "99.9"—the results.

function handleResponse() {
    if (HTTPObj.readyState == 4 || HTTPObj.readyState == "complete" ) {
        try {
            if (HTTPObj.status == 200) {
                var targ = document.getElementById(HTTPObj.targetId);
                targ.innerHTML = HTTPObj.responseText;
            } 
            else if (HTTPObj.readyState != "complete") {
                /* the test condition is for use with the iframe technique
                * presented elsewhere on this site. A readyState
                * value of 'complete' is returned by IE's iframe.
                */
                alert(HTTPObj.status + ": " + HTTPObj.status.Text);
            }
        } catch (e) {
            alert("Network error!\nPlease try later.");
        }
    }
} //eof handleResponse

The results are returned as a string and is the only content returned