vsriram92
vsriram92

Reputation: 663

How to insert joomla user name inside an AJAX based article?

I use joomla 3.x
I also use sourcerer plugin.

I have installed joomla in localhost at joomla30 directory.

I have my PHP & Ajax files in joomla30/cal directory.

I use sourcerer plugin and display php code. From that php code I use ajax to call another PHP file at joomla30/cal directory.

So data is get from the php file inside the cal directory. I need to get the user name from php code inside cal directory.

How to do that ?

I tried

$user = JFactory::getUser();
$user_id = $user->get('name');

but it didn work..

I get the error "Class JFactory not found."

Any other way to do this ?

Upvotes: 1

Views: 787

Answers (1)

Lodder
Lodder

Reputation: 19733

You can't by default add PHP to a Joomla article. You must use a plugin such as Sourcerer to achieve this.

Once installed and enabled, simply use the following:

{source}
<?php
    jimport( 'joomla.user.user' ); // add this line in
    $user = JFactory::getUser();
    echo $user->name;
?>
{/source}

Update:

Seeing as you're using a custom php file, you will need to import the Joomla framework like so:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).DS.'..' )); // change path accordingly

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe = JFactory::getApplication('site');

Hope this helps

Upvotes: 0

Related Questions