Post by twuncher » Mon Nov 08, 2010 9:07 pm

Hi I have been looking through the code and notice a few references to ajax and callback functions,

i just wanted some more information on how they work!

Example Code - the cart

Code: Select all

$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: 'remove=' + this.id,
			success: function (html) {
				$('#module_cart .middle').html(html);
				if (getUrlParam('route').indexOf('checkout') != -1) {
					window.location.reload();
				}
			}
		});
now i see that this sends an ajax post request to the url: 'index.php?route=module/cart/callback'
does this mean the function callback() in the controller/module/cart?
i notice if i just browse to it i get a blank screen instead of a 404. how come?

is this due to this bit of code in controller/module/cart?

Code: Select all

		if ($this->request->server['REQUEST_METHOD'] == 'POST') {

			if (isset($this->request->post['remove'])) {
	    		$result = explode('_', $this->request->post['remove']);
          		$this->cart->remove(trim($result[1]));
      		} else {
				if (isset($this->request->post['option'])) {
					$option = $this->request->post['option'];
				} else {
					$option = array();
				}

      			$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
			}
		}

if I wanted to create an ajax call for catagorys?
could i create a function called callback() in controller/product/catagory.php and point the ajax post function to
url: 'index.php?route=product/catagory/callback'

what code would i need to make it accept that request?

any help would be kindly appreciated
regards twun

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm

Post by Qphoria » Mon Nov 08, 2010 10:22 pm

Hi twun,

Yes you are correct in your thoughts about the callback.
index.php?route=module/cart/callback', means the callback function inside the catalog/controller/module/cart.php file
and yes the reason it doesn't load for you directly is that it needs to be POST where a normal url call is GET. You set that all up in that ajax function

Code: Select all

$.ajax({  <----- the setup for jquery's ajax function
         type: 'post', <----- the method of passing variables (POST or GET)
         url: 'index.php?route=module/cart/callback',  <----- the url to access the function
         dataType: 'html', <------------ the type of data (html or json)
         data: 'remove=' + this.id, <---------------- the actual POST or GET variable
         success: function (html) {  <----------- a javascript function that gets called if the main callback was successful
            $('#module_cart .middle').html(html);
            if (getUrlParam('route').indexOf('checkout') != -1) {
               window.location.reload();
            }
         }
      });

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by Xsecrets » Mon Nov 08, 2010 10:26 pm

for more information about the function you can look at the jquery site. It's a simple ajax function. you could also use get or post.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by twuncher » Mon Nov 08, 2010 10:34 pm

thanks for the reply Qphoria very helpful, making a bit more sense now :)

I will give it another go and I will post back if i need help understanding anything else.

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm
Who is online

Users browsing this forum: No registered users and 24 guests