Feature
Building a Drag-and-Drop Shopping Cart with AJAX
Creating an interactive shopping experience
Feb. 21, 2007 10:45 AM
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(
{action:'getCart'} ).request(); }
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">
Clear Shopping Cart</button>
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() {
if ( confirm('Are you sure you want to empty your cart?') ) clearCart();
}
function clearCart() {
objGateway.setListener('cartPacket_onReceive').setArguments( {action:'clearCart'}
).request();
}
Finally one more JavaScript function to be called when adding items to our cart:
function addToCart(upc) {
objGateway.setListener('cartPacket_onReceive').setArguments( { action:'addToCart',upc:upc }
).request();
}
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.
About Joe DanzigerJoe Danziger is a senior web applications developer with Multimax, Inc., a provider of Enterprise IT Services and Solutions supporting the critical missions of the Air Force, Army, Navy, and other Department of Defense components. He is certified as an Advanced Macromedia ColdFusion MX Developer, and also maintains the Building Blocks site (www.ajaxcf.com) dedicated to AJAX and ColdFusion, as well as DJ Central (www.djcentral.com), a Website serving DJs and the electronic dance music industry.
#22 |
King commented on 21 May 2008
Soon everything will be Ajax, just look how the major websites are adapting to use it.
|
#21 |
A commented on 7 May 2008
I think your right when you say that Ajax is the way of the future, although its is difficult to program, you can see yahoos and googles implementation of it.
|
#20 |
Scott commented on 2 Aug 2007
Hello N,
I had the same problem. Try changing:
to:
<cfif StructKeyExists(xnSearch.Items.Item, "MediumImage")>
|
#19 |
N commented on 14 Jul 2007
I'm following this tutorial to the letter as well and I also get this error in the code that retrieves the info from Amazon...
Element ITEMS.TOTALRESULTS.XMLTEXT is undefined in XNSEARCH
Is there a fix available or anything to get this to work. It looks good though.
N
|
#18 |
asdf commented on 10 Jul 2007
asdfsafs asdfasf asdfasf
|
#17 |
abcd commented on 10 Jul 2007
heloow asdf asdfs asdfasf biuiouer asdfaf boihoi awerew asdf
|
#16 |
Wally Kolcz commented on 22 Jan 2007
I loved your tutorial on how to make a drag and drop web cart, but I couldn't quite follow on how to put the code into the page to make it work. Do you have any finished files available for download to see how it is constructed?
Thanks
|
#15 |
BUses commented on 26 Dec 2006
Awesome content: L'augmentation d'AJAX pendant plusieurs mois passés a repris du monde de développement et a donné un second souffle au Web.
Merry Christmas of Poland!
|
#14 |
Simone from Italy commented on 20 Dec 2006
Hi, please add a link to index.cfm full compliled because It's very hard to have a union of all fragments introduced in your article. Thanks
|
#13 |
Danny commented on 31 Oct 2006
I'm following this tutorial to the letter and I get this error in the code that retrieves the info from Amazon...
Element ITEMS.TOTALRESULTS.XMLTEXT is undefined in XNSEARCH
Any ideas? Thanks for this tutorial!
|
#12 |
j j commented on 21 Sep 2006
Keeping up with the latest Web technologies is tough nowadays. Every week it seems new sites are launched that push the envelope further and further in terms of what can be accomplished using just a Web browser.
|
#11 |
How is user experience improved with dragging items into a shopping cart? ;) This is just developer feature candy. There are many other site components to focus your time on that will improve your application. Don't work so hard just to remove your 'add to cart' buttons.
|
#10 |
CFDJ News Desk commented on 4 Aug 2006
Keeping up with the latest Web technologies is tough nowadays. Every week it seems new sites are launched that push the envelope further and further in terms of what can be accomplished using just a Web browser.
|
#9 |
AJAXWorld News Desk commented on 4 Aug 2006
Keeping up with the latest Web technologies is tough nowadays. Every week it seems new sites are launched that push the envelope further and further in terms of what can be accomplished using just a Web browser.
|
#8 |
AJAXWorld News Desk commented on 4 Aug 2006
Keeping up with the latest Web technologies is tough nowadays. Every week it seems new sites are launched that push the envelope further and further in terms of what can be accomplished using just a Web browser.
|