I have three drop down in my module.I want to call ajax on drop down onChange() event in my joomla 2.5 module.
Country,State,City are drop down. user select country from first drop down when all state result display in second drop down using ajax.
How to do above functionality using AJAX in joomla 2.5 module.
Please help me.
There is no official way to do ajax from a module.
If you where to develop a component you could call your component with
index.php?option=com_yourcomponent&task=getjsondata&view=yourview&format=json
This way your the file views/yourview/view.json.php would be called and there you can get your data send it out echo json_encode( array("success"=>true) ); jexit()
please note the use of jexit() insted o exit()
If you really need to do ajax from a module here's a hack to import joomla in your files
<?php
if (! class_exists('JFactory') ) {
define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
$currentfolderlevel = 2;
array_splice($rootFolder,-$currentfolderlevel);
$base_folder = implode(DS,$rootFolder);
if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))
{
define( '_JEXEC', 1 );
define('JPATH_BASE',implode(DS,$rootFolder));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
// Joomla is loaded! horray!
$email = JRequest::getVar('email');
$tags = implode(",",JRequest::getVar('tags',array()));
$db = JFactory::getDbo();
$db->setQuery("INSERT INTO `#__newsletter_users` (email,tags) VALUES('$email','$tags')");
$db->query();
}
}
?>
create a file like the one above (save_email.php for me) in your module folder and call it directly. Here is a piece of my modules template:
$.get('<?php echo JUri::base()."modules/mod_newsletter/save_email.php?email=" ?>'+email, function(data) {
console.log(data);
modal.hide();
});
Related
in Joomla you can pass php variables from the template e.g. in less files. Now i try to do the same with scssphp (scss compiler from leafo.net)
So i think first i have to load the Joomla Framework in the style.php and define the variables $options = $this->params->get('option'); also there, but i´m not sure.
Is that possible at all? All my tests fail ...
Here an example:
require_once "scssphp/scss.inc.php";
use Leafo\ScssPhp\Server;
$directory = "scss";
Server::serveFrom($directory);
// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(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();
$option = $this->params->get('option');
If i now use #option in style.scss i get this error:
Parse error: Undefined variable $option: line: 14
I'm trying to instantiate a controller and execute some methods but no result :(
jimport('joomla.application.component.controller');
$controller = JController::getInstance('com_shop');
$controller->my_method($arg1, $arg2);
Any idea?
This won't work try: JControllerLegacy::getInstance('CONTROLLERNAME') assuming that the controller you are calling follows the naming convention
<COMPONENTNAME><Controller><CONTROLLERNAME> for example WeblinksControllerWeblink
following is controller instantiation code taken form lender. And you don't need to use jimport in Joomla 3 extensions. Joomla auto load all classes starting with J prefix.
<?php // No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//sessions
jimport( 'joomla.session.session' );
//load tables
JTable::addIncludePath(JPATH_COMPONENT.'/tables');
//load classes
JLoader::registerPrefix('Lendr', JPATH_COMPONENT);
//Load plugins
JPluginHelper::importPlugin('lendr');
//application
$app = JFactory::getApplication();
// Require specific controller if requested
if($controller = $app->input->get('controller','default')) {
require_once (JPATH_COMPONENT.'/controllers/'.$controller.'.php');
}
// Create the controller
$classname = 'LendrController'.$controller;
$controller = new $classname();
// Perform the Request task
$controller->execute();
I have the following PHP script file.php in my Joomla site:
$user = JFactory::getUser();
$usr_id = $user->get('id');
If I run it directly, using in HTML:
include_once "file.php";
It will get the user id, no problem.
However, if run it through an Ajax request:
$.ajax({
url: "other.php",
...
Where other.php is:
include_once "file.php";
I get the error:
Fatal error: Class 'JFactory' not found in file.php on line 3
Why? Any help!?
You need to import the Joomla library in order to use the JFactory class. To import the library, add the following to the top of your file:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) ).'/../..' );
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
I am trying to make a login system for an android application that works in with my 2.5 Joomla website. I am trying to do this by making a Joomla plugin which the android application sends post data to a php file which that then authenticates the user to see if the credentials are correct or not for the login. I have been trying to get this working all afternoon but it seems all my attempts of importing JFactory are failing.
I have the below code which bugs out at the first mention of JFactory. My attempt of importing it is not working either.
<?php
//needed to be commented out otherwise the android application cannot send data to the file
//defined('_JEXEC') or die;
define('_JREQUEST_NO_CLEAN', 1);
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE.'/includes/helper.php';
require_once JPATH_BASE.'/includes/toolbar.php';
require JPATH_BASE.'/library/joomla/factory.php';
$email = $_POST['email'];
$password = $_POST['password'];
file_put_contents("Log.txt", "got data: ".$email." and ".$password);
$credentials->email = $email;
$credentials->password = $password;
$responce;
//error occurs here
$db = JFactory::getDbo();
...
?>
I have looked at these other questions about importing JFactory but none of them are working for me:
- JFactory,JDatabase class not found in /var/www/joomla2.5/database.php
- Class 'JFactory' not found
- include Jfactory class in an external php file, Joomla
So my simplified question is how do I import JFactory or at least work around it in this situation? Anyone feeling smarter than me who would be so kind to answer the question :)
General Information:
running php 5.1.13
Joomla 2.5
Testing on wamp server (localhost)
I suggest to go the official Joomla way and bootstrap an application. What I did works pretty well and is the recommended Joomla way (I haven't tested the code below but it should give you a starting point as it was a copy paste from multiple sources in my code).
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
define('JPATH_BASE', dirname(dirname(dirname(__FILE__))));
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require JPATH_LIBRARIES.DS.'import.php';
require JPATH_LIBRARIES.DS.'cms.php';
JLoader::import('joomla.user.authentication');
JLoader::import('joomla.application.component.helper');
class MyJoomlaServer extends JApplicationWeb {
public function doExecute() {
$config = JFactory::getConfig();
$email = $_POST['email'];
$password = $_POST['password'];
$user = ...;//get the user with the email
file_put_contents("Log.txt", "got data: ".$email." and ".$password);
$authenticate = JAuthentication::getInstance();
$response = $authenticate->authenticate(array('username' => $user->username, 'password' => $password));
return $response->status === JAuthentication::STATUS_SUCCESS;
}
public function isAdmin() {
return false;
}
}
$app = JApplicationWeb::getInstance('MyJoomlaServer');
JFactory::$application = $app;
$app->execute();
Something along the lines works for me. More platform examples can be found here https://github.com/joomla/joomla-platform-examples/tree/master/web.
try this,
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
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();
$db = JFactory::getDbo();
It will work, you missed JFactory::getApplication('site');
Hope it helps..
My server with Joomla and API:
web/pruebasmario/joomla
web/public/api
I need use Jfactory..etc, functions plugin, componentes Joomla 2.5 in my Api.
I created a file accesjoomla.php in web/public/api:
<?php
include('../../pruebasmario/extjom.php');
$user = JFactory::getUser();
echo "Usuario " . $user->username . " con id: " . $user->id . " conectado a Joomla";
I created a file extjom.php in web/pruebasmario/joomla:
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
I look this: include Jfactory class in an external php file, Joomla
Access Joomla 2.5 from external script to get article by id
Joomla 2.5 Get User Data from External Script
BUT NO workk!!!!! :-((
Help me plez!
thanks!
EDIT: I managed to take a step.
Adding:
require_once (JPATH_BASE. DS. 'libraries'. DS. 'joomla'. DS. 'error'. DS. 'error.php');
I have no errors. But nothing appears. all white.
The return of the api is not returned.
What is happening?