I'm creating a Joomla component.
In a frontend's view I have a form with 2 drop down menus by choosing an option from the 1st, the options of the 2nd must change accordingly.
To do so I'm using AJAX.
I can send the request to the php function placed inside the controller.php file and the php function generates the correct output.
The problem is that Joomla framework put this output inside a page with all the meta tags, header, the template and so on as it should be shown on the browser before to give it back to AJAX.
You can see a screenshot from firebug of what I receive.
The div where I want to put the drop down menu is -div id="select-formatocarta"-, you can see that before the "select" I have many other unwanted things.
http://img695.imageshack.us/img695/7725/selectp.jpg
The result is that inside the div where I want to only put the "select" menu generated by the php function, I get whole page. Even if I can show the only menu passing the tmpl=component parameter from the AJAX request, the div height is like the whole page is inside it.
How can I receive the only php function's output without the Joomla template?
If that's not possible, how can I extract the only drop down menu from the responseText?
I can only use javascript, I don't have prototype or jquery available.
Exit.
So Joomla will evaluate your component code first, then add the header/footer via the templates system later (this is why components dynamically can add header lines for CSS etc). So all you need to do is exit immediately, stopping Joomla from being able to add the header/footer.
So in your controller code, you'd have something like:
<?php
function ajax_foo()
{
...code here...
$view->display();
jexit();
}
The built in PHP exit()
function would also work fine, but jexit()
is preferred.
You need to create a sub controller in your /components/com_yourcomponent/controllers/ folder called ajax.raw.php
You can then send ajax requests to it like this:
index.php?option=com_name&task=ajax.function_name&format=raw&var=value
- The "function_name" will execute a function of the same name inside the controller.
- The format=raw tells joomla not to include the template
- The var=value represents any parameters you would like to send and can be retrieved using JRequest::getVar()
You don't have to create a new controller for this. You can simply use your existing controller and append &format=raw to the URL. That switches off all output by Joomla. Creating a special controller for the ajax call is just something that could be good practice in case your component is big and difficult to maintain otherwise.
Using jexit(); simply to supress the output is absolutely bad.