In this chapter we looked at running our scripts server-side, how running scripts sever-side differed from running them client-side, and using ASP objects.
In Question 2 of Chapter 6 and continuing with Question 1 of Chapter 7, we created a form that allowed users to select their dream computer systems and to see the price change as they selected different components. Our task for this question is to enable the users to purchase the system they have selected. This involves the following conditions:
|
||
A: | I'm re-using the exercise answers from Chapter 7. First we need a top frameset page, identical to the one used in Chapter 7, Question 1. This time I've called it ch16Q2_TopFrame.htm.
<html> <head> </head> <frameset cols="55%,*"> <frame src="ch16Q2_PickSystem.htm" name="pickSystem"> <frame src="blank.htm" name="systemSummary"> </frameset> </html> We'll also need a blank HTML file, blank.htm, like so: <html> <body> </body> </html> Then we have the page that allows the user to pick the system, ch16Q2_PickSystem.htm. This is virtually identical to the version of PickSystem.htm in Chapter 7, apart from differences in two places. The first is at the top of the updateOrderDetails() function. function updateOrderDetails()
{
var total = 0;
var orderDetails = "<H3>Your selected system</H3>";
var orderForm = "";
var formElement;
The second is toward the end of the function. else { orderDetails = orderDetails + "Full Tower Case : $" + CompItems[formElement[2].value] total = total + parseFloat(CompItems[formElement[2].value]); } orderForm = orderForm + "<form action='ch16Q2_BuySystem.asp' method=post " + "name=form1 TARGET=_top><input type='submit' " + "value='Buy System'>" + "<input type='hidden' name='txtCost' " + " value='" + total + "'>" + "<input type='hidden' name='txtDescription' " + "value='" + orderDetails + "'></form>" orderDetails = orderDetails + "<P>Total Order Cost is $" + total + "</P>"; window.parent.systemSummary.document.open(); window.parent.systemSummary.document.write(orderDetails + orderForm); window.parent.systemSummary.document.close(); } Save this as ch16Q2_PickSystem.htm. Next we have our first new page, the one that is loaded when the Buy System button is clicked. <%@ language=JavaScript %> <html> <head> <title>Buy System</title> <script language=JavaScript> function checkForm() { var regExp = /[^\d ]/ var formValid = true; var theForm = document.form1; var elementIndex; var element; var nowDate = new Date(); var expMonth = parseInt(theForm.selMonth.value) var expYear = parseInt(theForm.selYear.value) for (elementIndex = 0; elementIndex < theForm.length; elementIndex++) { element = theForm[elementIndex]; if (element.type == "text") { if (element.value == "") { formValid = false; element.focus(); alert("Please complete all of the boxes"); break; } } } if (formValid == true && expYear <= parseInt(nowDate.getFullYear()) && parseInt(nowDate.getMonth()) > expMonth) { formValid = false; theForm.selMonth.focus(); alert("The credit card expiration date you selected has expired") } else if (regExp.test(theForm.txtCCNumber.value) == true) { formValid = false; alert("Please enter a valid credit card number"); theForm.txtCCNumber.focus(); theForm.txtCCNumber.select(); } return formValid; } </script> </head> <body> <H2>Purchase System</H2> <P>Please enter you name, address and credit card details below</P> <form action="ch16Q2_FinalStage.asp" method=post name=form1 onsubmit="return checkForm()"> <P> Your Full Name: <br> <input type="text" name=txtName> </P> <P> House Name/Number <br> <input type="text" name=txtHouse> </P> <P> Street: <br> <input type="text" name=txtStreet> </P> <P> City: <br> <input type="text" name=txtCity> </P> <P> <P> Credit Card Number: <br> <input type="text" name=txtCCNumber> <P> Expiration Date <br> <select name=selMonth> <option value="01">01</option> <option value ="02">02</option> <option value ="03">03</option> <option value ="04">04</option> <option value ="05">05</option> <option value ="06">06</option> <option value ="07">07</option> <option value ="08">08</option> <option value ="09">09</option> <option value ="10">10</option> <option value ="11">11</option> <option value ="12">12</option> </select> <select name=selYear> <% var yearCount; var nowYear = new Date(); nowYear = parseInt(nowYear.getFullYear()); for (yearCount = nowYear; yearCount < nowYear + 5; yearCount++) { Response.Write("<option value='" + yearCount + "'>") Response.Write(yearCount + "</option>"); } %> </select> <input type="hidden" name="txtCost" value="<%=Request.Form("txtCost")%>"> <input type="hidden" name="txtDescription" value="<%=Request.Form("txtDescription")%>"> <P> <input type="submit" value="Submit" name=submit1> </P> </form> </body> </html> Save this as ch16Q2_BuySystem.asp. Next we have the ch16Q2_FinalStage.asp page, which displays a summary of the user's order and order details and gives the user the chance to purchase or cancel the order. <%@ language = JavaScript %> <html> <head> <title>Final Summary</title> </head> <body> <H3>Order Summary</H3> <% var description = Request.Form("txtDescription").item; var cost = Request.Form("txtCost").Item var name = Request.Form("txtName").Item var houseNo = Request.Form("txtHouse").Item var street = Request.Form("txtStreet").Item var city = Request.Form("txtCity").Item var ccNumber = Request.Form("txtCCNumber").Item var expiryDate = Request.Form("selMonth") + "/" +Request.Form("selYear") Response.Write ("<P>" + description + "</P>") Response.Write ("<P>Will be delivered to </P>") Response.Write ("<P>Name : " + name + "</P>") Response.Write ("<P>At address</P>") Response.Write ("<P>" + houseNo + " " + street + "<br>") Response.Write (city) Response.Write ("<P>Your credit card " + ccNumber) Response.Write (" expiration date " + expiryDate) Response.Write (" will be debited by $" + cost) %> <form action="ch15Q2_PurchaseOrder.asp" method=post name=form1> <input type="hidden" name="txtName" value ="<%=name%>"> <input type="hidden" name="txtDescription" value ="<%=description%>"> <input type="hidden" name="txtCost" value ="<%=cost%>"> <input type="hidden" name="txtHouseNo" value ="<%=houseNo%>"> <input type="hidden" name="txtStreet" value ="<%=street%>"> <input type="hidden" name="txtCity" value ="<%=city%>"> <input type="hidden" name="txtCCNumber" value ="<%=ccNumber%>"> <input type="hidden" name="txtExpiryDate" value ="<%=expiryDate%>"> <input type="submit" value ="Purchase System" name=submit1> <P> <input type="button" value="Cancel Purchase" name=butCancel onclick="window.location.replace('Ch16Q2_TopFrame.htm')" > </P> </form> </body> </html> Finally, we have the ch16Q2_PurchaseOrder.asp page that does the actual purchase of the system, or at least it would if this were not an example! <%@ language=JavaScript %> <html> <head> <title>Order Completed</title> </head> <body> <H3>Thank you for your order <%=Request.Form("txtName")%> <br> it'll be with you shortly </H3> </body> </html> Save this page as ch16Q2_PurchaseOrder.asp. Let's take a brief look at how it all works; I won't cover the bits we did in Chapters 6 and 7, just the additions. First in ch16Q2_PickSystem.htm, we added the following code: orderForm = orderForm + "<form action='Ch16Q2_BuySystem.asp' method=post " + "name=form1 TARGET=_top><input type='submit' " + "value='Buy System'>" + "<input type='hidden' name='txtCost' " + " value='" + total + "'>" + "<input type='hidden' name='txtDescription' " + "value='" + orderDetails + "'></form>" When the user clicks the update button to view the system she has chosen, not only is the HTML necessary to display the system written to the page, but now we also write an HTML form that contains a button called Buy System and two hidden input boxes, which we discussed in Chapter 6, that contain details of the system the user wants and the cost. When the Buy System button is clicked, the form will submit to the first of our new pages, ch16Q2_BuySystem.asp. Let's start at the top of ch16Q2_BuySystem.asp. First, since we want to use JavaScript as our server-side scripting language we have
<%@ language=JavaScript %>
Next we have a block of client-side script which contains the function checkForm(). This function does some basic checks to see that all the input boxes have been filled in, a valid date has been selected for the credit card expiration date, and the credit card number only contains permitted characters (that is, numbers and spaces). If any of these tests fails, we alert the user and stop the form submit from continuing by returning false from the function to the event handler it was called from. Next we come to the form itself, where users enter their details, such as name, address, and credit card number. Most of the form should be fairly self-explanatory except this: the credit card expiration year select box. <select name=selYear> <% var yearCount; var nowYear = new Date(); nowYear = parseInt(nowYear.getFullYear()); for (yearCount = nowYear; yearCount < nowYear + 5; yearCount++) { Response.Write("<option value='" + yearCount + "'>") Response.Write(yearCount + "</option>"); } %> </select> Rather than write in the years ourselves, we've used some server-side script to create the <option> tags with a series of five years starting from the current year. The advantage of that is we don't need to change our page for it to be up-to-date. So even if you're reading this appendix in 2030, the years will still be valid—that is, 2030, 2031, 2032, 2033, and 2034. I wrote the month's select box in myself, and although it would save typing to have JavaScript create them, it would add an extra and unnecessary load on the server for it to dynamically create them for each page load. If you're wondering why select rather than text input boxes have been chosen to get the expiration date, it's because this helps prevent validation errors. If we asked the user for the expiration date, there is a danger she will enter it in an unexpected or invalid format, and we'd have to write lots of code to check for this situation. With select boxes, the user has no choice but to get the format right. Finally we have two hidden input boxes, their purpose being to hold the information (the system description and cost) posted from the last page. <input type="hidden" name="txtCost" value="<%=Request.Form("txtCost")%>"> <input type="hidden" name="txtDescription" value="<%=Request.Form("txtDescription")%>"> Hidden input boxes are a very useful way of transferring information from page to page when using form submits, because you have no need for server variables or cookies. Next we come to ch16Q2_FinalStage.asp, where we display a summary of what the user has entered so she can check the order details before finally committing to it. First we have the server-side code that retrieves the posted form details and uses it to write a summary to the page before it's sent to the client browser. <% var description = Request.Form("txtDescription").item; var cost = Request.Form("txtCost").Item var name = Request.Form("txtName").Item var houseNo = Request.Form("txtHouse").Item var street = Request.Form("txtStreet").Item var city = Request.Form("txtCity").Item var ccNumber = Request.Form("txtCCNumber").Item var expiryDate = Request.Form("selMonth") + "/" +Request.Form("selYear") Response.Write ("<P>" + description + "</P>") Response.Write ("<P>Will be delivered to </P>") Response.Write ("<P>Name : " + name + "</P>") Response.Write ("<P>At address</P>") Response.Write ("<P>" + houseNo + " " + street + "<br>") Response.Write (city) Response.Write ("<P>Your credit card " + ccNumber) Response.Write (" expiry date " + expiryDate) Response.Write (" will be debited by $" + cost) %> Finally we have a form, most of which is hidden input boxes that contain all the order details received so far. When the user clicks the Purchase button, all the information will be submitted in one go. It's much easier to deal with all the information for a transaction at once rather than try to do it in little stages. Otherwise we'd need to deal with the possible situation where the user changes her mind halfway through or where her computer, browser, or connection terminates unexpectedly. <form action="Ch16Q2_PurchaseOrder.asp" method=post name=form1> <input type="hidden" name="txtName" value ="<%=name%>"> <input type="hidden" name="txtDescription" value ="<%=description%>"> <input type="hidden" name="txtCost" value ="<%=cost%>"> <input type="hidden" name="txtHouseNo" value ="<%=houseNo%>"> <input type="hidden" name="txtStreet" value ="<%=street%>"> <input type="hidden" name="txtCity" value ="<%=city%>"> <input type="hidden" name="txtCCNumber" value ="<%=ccNumber%>"> <input type="hidden" name="txtExpiryDate" value ="<%=expiryDate%>"> <input type="submit" value ="Purchase System" name=submit1> <P> <input type="button" value="Cancel Purchase" name=butCancel onclick="window.location.replace('Ch16Q2_TopFrame.htm')" > </P> </form> The very last page is ch16Q2_PurchaseOrder.asp, in which we simply say thank you for buying the system. In reality this is where all the order processing would be done, such as placing the order details in a database, processing the credit card, and ensuring that the orders department is told to send the system. |