Dmitry
Dmitry

Reputation: 4373

coldfusion session refresh

Is there a way to refresh coldfusion session on the page without reloading the page? Let's say I had 3 items in my shopping cart. Now I want to remove one of the items by clicking "remove" link next to a product. I created delete_item.cfm that removes a particular item from cart using jquery ajax. Now I want my shopping cart to display only 2 items without reloading the page. Here is my code.

<CFIF ISDEFINED("ProductID")>
    <!--- Find where in the basket it is --->
    <CFSET ItemPosition = ListFind(session.StoreItems,ProductID)>       
    <CFSET session.StoreItems = ListDeleteAt(session.StoreItems, ItemPosition, ",")>
    <CFSET session.StoreItemsQty = ListDeleteAt(session.StoreItemsQty, ItemPosition, ",")>

Upvotes: 1

Views: 1421

Answers (2)

Ken Redler
Ken Redler

Reputation: 23943

This has little to do with ColdFusion specifically, and more to do with a very common Ajax design pattern. You've got most of it right; here's the general idea:

  1. User clicks [delete].

  2. A JavaScript handler function sends the ID of the item to be deleted to your delete_item.cfm handler on the server. Example:

    $('a.deletelink').click( function(e){
      e.preventDefault();
      $.ajax({ 
        url : '/handlers/delete_item.cfm',
        data : $(this).data('id'),
        type : 'post',
        success : /* see below */
      });
    });
    
  3. On the server, another function retrieves an updated view of the region of the page affected by the change -- now without the deleted item. delete_item.cfm calls this function and returns the updated view information to the Ajax requester. This could take the form of:

    • The raw data, perhaps in the form of a JSON string, or...
    • A fully rendered HTML version of the region to be re-drawn.
  4. In the success handler of the Ajax call, the updated view information is received. You then:

    • Loop over the JSON data and build the appropriate HTML in JavaScript, then drop it into your container area (perhaps using a templating engine), or...
    • Drop in the fully rendered HTML as supplied from delete_item.cfm, replacing the older version that originally contained the item you're deleting.

    Example:

    /* success handler from above */
    function(data){ // data is what's returned from delete_item.cfm
                    // Assuming it's fully rendered HTML post-deletion:
      $('#container_of_your_list')
        .html( data ) // drop in new data
        .effect('highlight',{},2000); // visual feedback to user
    }
    

Upvotes: 6

Dan Short
Dan Short

Reputation: 9616

Absolutely. When you make an AJAX request, you're making the request as the user... so if you make any changes to session, it will make those changes on the user's session. That being said, if you want to redraw the cart page, you'll need to do all of that with client-side javascript, just like you're making the AJAX call.

Upvotes: 1

Related Questions