Developer forum

Forum » Development » Submit to basket

Submit to basket


Reply

Is it possible to make a "submit to basket" function that will NOT reefresh the page?

 

Anyone had any luck?

 

Rasmus Vork


Replies

 
Reply

You can submit either to an iframe or using javascript to send the request. Would that do?

 
Reply
Sorensen wrote:

You can submit either to an iframe or using javascript to send the request. Would that do?

An iFrame was my initial idea, and it could work. 

Thing is though - i need this functionality for a flash application i am developing, so a javascript sollution would be a smarter and more refined choice. 

 

Do you have any examples on how to do this with javascript? All i see  in your default templates are a form submission  or an url command.

 

Rasmus Vork

 
Reply

I quickly brewed this together, so please don't consider this a complete example, but something like this would work:

 

function AddToBasked(){
 str = "CartCmd=add&ProductID=PRODXX&VariantID=Vx&Quantity=1"
 var req = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");

 req.open("POST", "Default.aspx", false);
 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 req.setRequestHeader("Content-Length", str.length);
 req.onreadystatechange = function (){
  if (req.readyState == 4){
   if (req.status == 200){
    alert("Product added to basket");
   }else if(req.status == 500){
    alert("Server Error:\n" + req.responseText);
   }
  }
 }

 req.send(str);
}

 

Interesting case you're mentioning. I'd love to hear more about it when you're finished with it if that's possible.

 
Reply
Sorensen wrote:

I quickly brewed this together, so please don't consider this a complete example, but something like this would work:

 

function AddToBasked(){
 str = "CartCmd=add&ProductID=PRODXX&VariantID=Vx&Quantity=1"
 var req = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");

 req.open("POST", "Default.aspx", false);
 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 req.setRequestHeader("Content-Length", str.length);
 req.onreadystatechange = function (){
  if (req.readyState == 4){
   if (req.status == 200){
    alert("Product added to basket");
   }else if(req.status == 500){
    alert("Server Error:\n" + req.responseText);
   }
  }
 }

 req.send(str);
}

 

Interesting case you're mentioning. I'd love to hear more about it when you're finished with it if that's possible.

Thank you. I will try  to mess around a bit with this. Next issue is refreshing the cart paragraph async to the rest of the page. ;)

 

Rasmus Vork

 
Reply

Well, the req.responseText contains the HTML returned by the server, so if you set the target page to a page containing only the basket (in HTML or XML) it's pretty straight forward to resolve the contents of the basket and use it how you like. req also provides a responseXml that may be earsier to work with if you follow the XML path. I just use the responseText in the example because the original destination page returned a javascript that I consumed on the caller page using eval().

 
Reply
Sorensen wrote:

Well, the req.responseText contains the HTML returned by the server, so if you set the target page to a page containing only the basket (in HTML or XML) it's pretty straight forward to resolve the contents of the basket and use it how you like. req also provides a responseXml that may be earsier to work with if you follow the XML path. I just use the responseText in the example because the original destination page returned a javascript that I consumed on the caller page using eval().

Not really sure i understand?

 

What is the target page you speak of? I've already made a page containing a blank master and content template and only the cart template returns any html.

 

Also it doesn't seem that the responseXml works for me?

 

Rasmus Vork

 
Reply

The target page is the page that is the target of my action. In the example req.open calls Default.aspx which is the target for this request. If you redirect this request using the Redirect parameter in query string, to a page containing only your basket (the blank one you describe), the responseText will hold the html you need for displaying the basket, and you'll have both the adding to basket and retreiving the basket html in one single call.

 

This is basically the same that Ajax does, only we code it by hand here. It's a lot more fun:). I'll bet you'll find an explanation why responsexml doesn't work in the documentation. May be as simple as me doing incorrect casing, but try googleing for some javascript with Microsoft.XMLHTTP or XMLHttpRequest if you're using firefox for further info on these techniques.

 
Reply
Sorensen wrote:

The target page is the page that is the target of my action. In the example req.open calls Default.aspx which is the target for this request. If you redirect this request using the Redirect parameter in query string, to a page containing only your basket (the blank one you describe), the responseText will hold the html you need for displaying the basket, and you'll have both the adding to basket and retreiving the basket html in one single call.

 

This is basically the same that Ajax does, only we code it by hand here. It's a lot more fun:). I'll bet you'll find an explanation why responsexml doesn't work in the documentation. May be as simple as me doing incorrect casing, but try googleing for some javascript with Microsoft.XMLHTTP or XMLHttpRequest if you're using firefox for further info on these techniques.

With the chance of sounding completely hopeless... The Redirect parameter in the query string??

 

Can't i just add ?ID=XXXX after Default.aspx at req.open? or add the same at the start of the str variable? Doesn't seems so. But then what are we talking about here?

 

Rasmus Vork

 
Reply

Add a redirect variable to the query string like: Default.aspx?Redirect=somepage.aspx.

 

But yes, you're right, adding the ID parameter to the str variable will work as well, and looks to be a more logic choise:)

 
Reply
Sorensen wrote:

Add a redirect variable to the query string like: Default.aspx?Redirect=somepage.aspx.

 

But yes, you're right, adding the ID parameter to the str variable will work as well, and looks to be a more logic choise:)

Hi Lars,

 

I don't think your sample will work. After every "CartCmd" it looks like the Dynamicweb core auto redirects back to the page where you came from.

 

I've already made a feature request for this; because we'd really like to make AJAX enabled shopping carts.

 

Kind Regards,

Emil

 
Reply

Hi Emil,

 

It will redirect unless you escape it by adding a Redirect parameter to the query string. It should look like this:

 

Default.aspx?CartCmd=add&ProductID=xx&Quantity=y&Redirect=Default.aspx?ID=zz

 
Jacob Jensen
Reply
Anyone ever got a working solution for this? I'm really interested in a solution which works without I need to guess my way through the above discussion ;-)
I just need this: When the "add to cart" button on a product is clicked, it should update the cart without reloading the page and the element containing the Quantity and Total cart amount should be updated...

 
Pavel Volgarev
Reply
Hi guys,

I've just posted an article on my blog regarding managing the shopping cart using AJAX:

Managing your shopping cart using AJAX

-- Pavel

 

You must be logged in to post in the forum