On my Joomla 2.5.x site, I want to add links, that opens Fancybox window, with mail form, in which the user enter mail and address. On submit, the plugin will validate and send the mail to the correct recipient.
I wrote the plugin, and stumble into a problem: How to get Ajax to call the php script that is doing the db staff and sending the mail.
If I use the following url:
$.ajax({
type: 'POST',
url: ''/my_site/plugins/content/my_plugin/sendmessage.php',
...
The php script is out of Joomla reference, It closes due to:
defined('_JEXEC') or die();
And Joomla functions are not working. There are workaround for this like adding the following staff to the php script:
<?php
define( '_JEXEC', 1 );
// JPATH_BASE should point to Joomla!'s root directory
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();
$user =& JFactory::getUser();
$session =& JFactory::getSession();
?>
But I am not sure about the security of this workaround. And I prefer clean solutions. I searched a lot, and found out that the right way is to create a php function, add it to the controller and call it from Ajax in the following way:
$.ajax({
type: 'POST',
url: 'index.php?option=com_mycomponent&format=raw&task=mytask';
...
But is this way is possible only for a Component ? What I need is a Plugin. How can I run a simple ajax call to a php function/script in a Plugin ? Any help will be appreciated, I am looking for a solution for quit some time.