1
votes

I'm trying to retrieve the "current" article id from an external script in Joomla! 2.5 Firstly, I included Joomla core files:

define( '_JEXEC', 1 );
defined('_JEXEC') or die('Restricted access');
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' );

Then, initialized the session:

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

Now, I know that for Joomla 2.5 I have to use Jinput instead of JRequest (deprecated), so:

$jAp = JFactory::getApplication();
$jInput = $jAp->input;

As already told, I'm trying to retrieve the article id. I tried many instances but nothing seems to work.

$id = $jInput->get('id', 0); // doesn't work
$idInt = $jInput->getInt('id',0); //doesn't work

Also with JRequest... I can't get current article id.

This works only if I would request data for logged in users, example:

$user = JFactory::getUser();
echo $user->username; // this works...

What I am missing? Where is my fault?

Thanks a lot to all!

1
Have you tried getting it from the super global through $jInput->get->get("id")? - Lance
Hello Lance. Yes, I tried it and it doesn't work. I tried also through $jInput->get->get('id',null,null). Nothing. Thank you for your reply. - David Madhatter
What do You mean that you try to get current article id. If article is displayed you already use framework so you can also use usual framework functions. So code like this: JRequest::getInt('id') (it is deprecated but still works) should return current article id. Same as $_REQUEST['id']. How that article is displayed? - Artur Stępień
Hello Arthur. What you see (original question) is part of an external AJAX file (ajax_request.php). JRequest::getInt('id') and $_REQUEST['id'] don't work in this ajax file (called by a function: jQuery(document).ready(function($){ $('#divid').load('/plug/ajax_request.php'); }); ) - David Madhatter
In your ajax request, you need to pass the article ID as well. Your jQuery code probably has access to the joomla article ID. If you have SEF enabled, you might not be able to get the ID from the URL. You might have to use php to write some javascript to define a variable with the article ID. - Lian

1 Answers

0
votes

Debugging AJAX can be complex. I often find adding some logging in my handler helps a lot, for example adding the following to the start of your code...

# logging of all hits
$log_file = realpath(dirname(__FILE__)) . '/ajax_debug.log';
$fh = fopen($log_file, 'a') or die();
$log_string = "Backend Hit \n" . date("Y-m-d H:i:s") . "\n";
$log_string .= "POST: " . print_r($_POST, true) . "\n";
$log_string .= "GET: " . print_r($_GET, true) . "\n";
$log_string .= "Hit by: " . $_SERVER['REMOTE_ADDR'] . "\n";
$log_string .= "\n\n\n";
fwrite($fh, $log_string);
fclose($fh);  

You didn't mention how/where your AJAX code was implemented. It sounds like it might be an implementation outwith Joomla. I would tend to put this in a plug-in if it is quite stand-alone, though module or component AJAX handlers also work well in Joomla. There are some good docs and code at https://docs.joomla.org/Using_Joomla_Ajax_Interface