|
|
SYS-CON MAGAZINES |
TOP LINUX LINKS YOU MUST CLICK ON Feature
Building a Drag-and-Drop Shopping Cart with AJAX
Creating an interactive shopping experience
By: Joe Danziger
Feb. 21, 2007 10:45 AM
Digg This!
Page 2 of 2
« previous page
Note that you can chain these methods together. For example, it is perfectly acceptable to write: objGateway.resetArguments().setArguments( { state:'NY' } ).request() Using what we know now, let's take another look at our updateCart() function that we're calling onLoad.
function updateCart() { objGateway.setListener('cartPacket_onReceive').setArguments( The function chains together several commands. It sets the listener to "cartPacket_onReceive". That means that we'll execute this JavaScript function whenever data is returned from our gateway. This function handles the generation of our table body that contains our cart rows (see Listing 2). In our updateCart() function, we're also passing in an argument: action=getCart. This is going to be passed through to our cart.cfm gateway page. The full text of the gateway page is displayed in Listing 3. We're passing in the action variable with a value of "getCart". This gets passed to our cart.cfm gateway page and causes the user's session cart to be returned as a query object. Whenever we need to update our cart to add or delete rows, we'll set our listener to 'cartPacket_onReceive' and then redraw the table body. When we created our shopping cart on screen, we added the following button to clear our cart:
<button onclick="emptyCartButton_onClick()" id="emptyCartButton"> We'll add two JavaScript functions to go along with that button. The first will confirm the delete and the second will issue a call to remove the items and redraw the cart:
function emptyCartButton_onClick() { Finally one more JavaScript function to be called when adding items to our cart:
function addToCart(upc) { Now that we have our addToCart() function coded, add the line "addToCart(element.id);" right before the Element.hide call in the shopping cart droppable. This will execute our addToCart() function and redraw the shopping cart when an item is dropped onto it. And that's all there is to it! With just 150 lines of code, we were able to create an interactive, drag-and-drop shopping experience that many did not think was possible using just the browser. Page 2 of 2 « previous page
Subscribe to our RSS feeds now and receive the next article instantly!SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS |
|
|||||||||||