160
Chapter 3
function sendRequest(url) {
request.onreadystatechange = serveDrink;
request.open(“GET”, url, true);
request.send(null);
}
function orderCoffee() {
var name = document.getElementById(“name”).value;
var beverage = getBeverage();
var size = getSize();
var url = “coffeemaker.php?name=” + escape(name) +
“&size=” + escape(size) +
“&beverage=” + escape(beverage) +
“&coffeemaker=1”;
sendRequest(url);
}
We’ll write the functions to get the size and
beverage requested in just a few pages.
sendRequest() sets up the callback
function for the server response
(serveDrink) and sends a GET
request to the URL passed in from
makeFreshCoffee().
sending a request to brew coffee
Writing JavaScript to send the request
Now let’s write the JavaScript to send the request
to the coffee-making script:
JavaScript
PHP script
HTML form
All this code should go in
your copy of coffee.js.
orderCoffee() builds the request URL,
using the coffeemaker.php script and all
the information it got from the coffee
order form.. It requests the rst coffee
maker be used to brew the coffee.
coffee.js