Section 5.22. EXERCISE SOLUTIONS


5.22. EXERCISE SOLUTIONS

Just do It Solutions

It's time to make Break Neck an even better place for ordering pizza. Look in the chapter05/ breakneck/ folder in the book's examples, and you'll find the Break Neck order form, complete with HTML, CSS, and PHP. You'll need to open up pizza.html, and change the form to run submitOrder() when "Order Pizza" is clicked, instead of submitting directly to the placeOrder. php script on the Break Neck server.

As long as you're making improvements to Break Neck, go ahead and move all of the JavaScript out of pizza.html. You can use ajax.js, which you wrote in Chapter 3, for creating the request object. Then, create a new JavaScript file called pizza.js. Move your Break Neck functions- getCustomerInfo(), updatePage(), and submitOrder()-into this file. Don't forget to add <script> elements that link to these files in your HTML!

 Here's just the <head> section of pizza.html. <html>  <head>   <title>The New and Improved Break Neck Pizza</title>   <link rel="stylesheet" type="text/css" href="breakneck.css" />   <script type="text/javascript" src="/books/2/850/1/html/2/ajax.js"> </script>   <script type="text/javascript" src="/books/2/850/1/html/2/pizza.js"> </script>Here are the two external JavaScript files you should have created.  </head>There's no other JavaScript in the HTML in this new version. 


 Here's ajax.js. You should have this file from Chapter 3. var request = null; try {   request = new XMLHttpRequest(); } catch (trymicrosoft) {   try {     request = new ActiveXObject("Msxml2.XMLHTTP");   } catch (othermicrosoft) {     try {       request = new ActiveXObject("Microsoft.XMLHTTP");     } catch (failed) {       request = null;     }   } } if (request == null)    alert("Error creating request object!"); 


 Here are your Break Neck-specific functions; you should have these in a fille called pizza.js. function getCustomerInfo() {   var phone = document.getElementById("phone").value;   var url = "lookupCustomer.php?phone=" + escape(phone);   request.open("GET", url, true);   request.onreadystatechange = updatePage;   request.send(null); } function updatePage() {   if (request.readyState == 4) {     if (request.status == 200) {       var customerAddress = request.responseText;       document.getElementById("address").value = customerAddress;     } else       alert("Error! Request status is " + request.status);   } } function submitOrder() {   var phone = document.getElementById("phone");   var address = document.getElementById("address");   var order = document.getElementById("order");   var url = "placeOrder.php?phone=" + escape(phone) +             "&address=" + escape(address) +             "&order=" + escape(order);   url = url + "&dummy=" + new Date().getTime();   request.open("GET", url, true);   request.onreadystatechange = showConfirmation;   request.send(null); } 





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net