Zencart + Ajax made easy (part 1)
In the first part, I will address several server-side isses and possible solutions I encounter while trying to use Ajax in Zencart
- Which format should be used to return data?
- How to Implement ajax on current zencart pages/functions without breaking the default behaviors?
- How to allow fall-back to the traditional methods when the browser does not support javascript?
- How to load and return only certain parts to reduce load time(which is one of the main points of using Ajax anyway)
Lets use the contact us page as an example, throughout the tutorial we will build a simple Ajax based contact form.
JSON and Ajax
After playing with multiple data formats, I finally chose JSON as THE one that I will be using mainly in my ajax related projects. I think it’s a simple and easy to understand format, so I use it. If you want to use xml or plain text or any other format, it’s up to you. Just keep in mind that you have to be consistent, your choice of data format will affect both your server-side and client-side code.
After deciding to use JSON, I then came up with the general structure my returned data should have
- status: this will tell if the request made was a ‘success’, ‘error’, or do I need to do ‘redirection’ etc…
- message: this will include ‘success’, ‘error’, ‘warning’ messages etc. The content of Zencart messageStack will be returned here
- content: content will be an array containing multiple content ‘blocks’ such as ‘leftColum’, ‘rightColumn’ etc so that you can update multiple parts of the current page.
- url and redirect_type will be used mainly for redirection purpose
Houston, we have contact
To make sure that we support “graceful fallback” when the browser does not support javascript, we first need to detect if the request made to the server is an Ajax request.
Currently I do this by checking the HTTP_X_REQUESTED_WITH header and optional $_REQUEST field
if(((!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') || $_REQUEST['isajaxrequest'] == 1)
Gotta Catch ‘Em All
Now that we decide to use JSON, we have to make sure the content returned is in JSON format, no extra character can be returned, otherwise we will not be able to decode the returned data correctly.
I achieved this by turning output buffering on before Zen prints out anything, and then clearing the output buffer after Zen has printed out everything.
Everything falls into place
So far, this is what we have:
class Ajax{
private $status = false;
public function start(){
// set status
if(((!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') || $_REQUEST['isajaxrequest'] == 1){
$this->status = true;
ob_start();
}
}
public function end(){
if($this->status){
ob_end_clean();
}
}
}
And now we have to edit zencart’s index.php and put $Ajax->start(); right after calling application_top and $Ajax->end(); right before calling application_bottom.
*
(You will find a complete package to download at the end of this Ajax tutorial series)
Related posts:
- Zencart + Ajax made easy (part 3) As an update to the long delayed series Zencart Ajax...
- Zencart + Ajax made easy (part 2) This is the second part of the series “Zencart +...
- Zencart Ajax Checkout In our continuous effort to help shop owners convert more...



thanks, looking forward to part II
I wonder where you put the Ajax class to call it in the index.php?
@jajaklar82: If you put it in includes/classes and then you use Zencart’s auto_loaders then you can load the class first. (you will see that we use the $Ajax object right after require(‘includes/application_top.php’);)
HI,
Any update on the tutorial download?
I am new to zencart and wouldnt mind learning
Late reply indeed, but the module is complete and being used in our Ajax Checkout module. I would just put the whole php code part here if you guys are interested in it.
Regards