How to use Joomla features through AJAX file
AJAX is very popular now. So if you have a Joomla-based site, you can meet a problem of how to add AJAX there. This article will describe how to use features of Joomla 1.5 directly through AJAX file and will provide the code for this.
Of course, you can use AJAX in index.php file, but this can cause a lot of problems. Therefore I advise you to work directly with AJAX file and use all Joomla features there.
So, open AJAX file and make initialization of the required classes, variables and libraries:
<?php
global $mosConfig_absolute_path, $mosConfig_live_site, $mosConfig_lang, $database, $mosConfig_mailfrom, $mosConfig_fromname;
require_once (‘configuration.php’);
define ( ‘_JEXEC’, 1 );
define ( ‘JPATH_BASE’, dirname ( __FILE__ ) );
define ( ‘DS’, DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . ‘includes’ . DS . ‘defines.php’);
require_once (JPATH_BASE . DS . ‘includes’ . DS . ‘framework.php’);
$mainframe = & JFactory::getApplication ( ‘site’ );
$mainframe->initialise ();
JPluginHelper::importPlugin ( ‘system’ );
$mainframe->triggerEvent ( ‘onBeforeStart’ );
$lang = & JFactory::getLanguage ();
$mosConfig_lang = $GLOBALS [‘mosConfig_lang’] = strtolower ( $lang->getBackwardLang () );
$mosConfig_live_site = str_replace ( ‘/administrator/components/com_virtuemart’, ”, JURI::base () );
$mosConfig_absolute_path = JPATH_BASE;
if (file_exists ( $mosConfig_absolute_path . ‘/language/’ . $mosConfig_lang . ‘.php’ )) {
require_once ($mosConfig_absolute_path . ‘/language/’ . $mosConfig_lang . ‘.php’);
} elseif (file_exists ( $mosConfig_absolute_path . ‘/language/english.php’ )) {
require_once ($mosConfig_absolute_path . ‘/language/english.php’);
}
When done, save the file. All regular Joomla features will be used in this file then. Thus you don’t need to create the unnecessary code because you already have access to the whole site through the AJAX file.