Calling AJAX from javascript in Joomla

2,928 views
Skip to first unread message

bernat

unread,
Jan 18, 2010, 4:43:37 PM1/18/10
to Joomla! General Development
Hi guys,

I'm trying to call an Ajax function from JavaScript in Joomla and I
didn't succeed.
I use the following "standard" code:

function myfunction()
{
var query;

if(window.XMLHttpRequest) { // Firefox, Safari, etc
query = new XMLHttpRequest();
}
else if(window.ActiveXObject) { // IE
query = new ActiveXObject("Microsoft.XMLHTTP");
}

query.onreadystatechange = function()
{
if(query.readyState == 4) {
if(query.status == 200) {
eval(query.responseText);
}
}
}

alert('I'm gonna call');

query.open('GET','ajax/ajaxfunction.php');
query.send(null);
}

I think it should works but I haven't any results. I didn't found
documentation about this and I don't know what I'm doing wrong. Can
anybody help me?

Thanks,
bernat


dukeofgaming

unread,
Jan 18, 2010, 5:10:12 PM1/18/10
to joomla-de...@googlegroups.com
Hi, perhaps this can help:


Regards,

David

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.




Herman Peeren

unread,
Jan 18, 2010, 5:30:59 PM1/18/10
to Joomla! General Development
Hi,

<b>1. Serverside</b>
You now called a function server-sided, that was not within the
Joomla!-framework. That is not necessary, but it is what you would
expect from a Joomla!-site. What do you get back if you would call
that ajax/ajaxfunction.php directly in your browser? That is often a
good method to debug Ajax-applications: just call the url on the
server directly. On this page (in Dutch, but maybe the code can help)
I give an example of some simple Ajax serverside-code within Joomla!,
that gives back some JSON, with and without MVC:
http://forum.joomlacommunity.eu/showthread.php?t=4871&page=2

<b>2. Clientside</b>
You could use Mootools or JQuery to handdle the Ajax-request client-
side.
- the way you code all JavaScript yourself now is also descripted in
http://www.wrox.com/WileyCDA/Section/Adding-Ajax-to-Joomla-Web-Applications.id-306544.html
- about the use of MooTools and Ajax, see also http://forum.joomla.org/viewtopic.php?p=628617
(and the links you find there)
- some more on MooTools & Ajax: http://demos.mootools.net/Ajax
- with JQuery also very well possible. See a.o.:
http://extensions.joomla.org/extensions/core-enhancements/scripts/7230

Good luck!

Matt Thomson

unread,
Jan 18, 2010, 6:08:44 PM1/18/10
to Joomla! General Development
I prefer to use mootools to do ajax, it seems unnescessary to do all
the XMLHttpRequest stuff. With mootools it is as easy as:

$('myFormId').send
({
onComplete: function(response)
{
alert(response);
}
}

On Jan 19, 11:30 am, Herman Peeren <herman.pee...@gmail.com> wrote:
> Hi,
>
> <b>1. Serverside</b>
> You now called a function server-sided, that was not within the
> Joomla!-framework. That is not necessary, but it is what you would
> expect from a Joomla!-site. What do you get back if you would call
> that ajax/ajaxfunction.php directly in your browser? That is often a
> good method to debug Ajax-applications: just call the url on the
> server directly. On this page (in Dutch, but maybe the code can help)
> I give an example of some simple Ajax serverside-code within Joomla!,
> that gives back some JSON, with and without MVC:http://forum.joomlacommunity.eu/showthread.php?t=4871&page=2
>
> <b>2. Clientside</b>
> You could use Mootools or JQuery to handdle the Ajax-request client-
> side.

> - the way you code all JavaScript yourself now is also descripted inhttp://www.wrox.com/WileyCDA/Section/Adding-Ajax-to-Joomla-Web-Applic...
> - about the use of MooTools and Ajax, see alsohttp://forum.joomla.org/viewtopic.php?p=628617

bernat

unread,
Jan 19, 2010, 12:42:47 PM1/19/10
to Joomla! General Development
Ok guys,

First of all, thank you for your replies. I tried a lot of things and
I didn't succeed so I don't know what to do....

I want to use Ajax on the Client part so I started with
http://p2p.wrox.com/content/articles/adding-ajax-joomla-web-applications
The example is very easy but is a little bit different of what I want
to do. I've a PHP page that calls a Javascript and using Ajax (call
another PHP script) i want to change some elements in the document. I
don't understand why inside of joomla is so difficult to do that. If I
use the same code without joomla all works fine.

Matt, you wrote about Mootools, could you give me more information
(examples or details) of how to use it ? I would appreciate that.

I also installed the jQuery extension but when I try to execute a
piece of code like this:
$.ajax({
type: "POST",
url: "PageName.php",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});

The fireBug returns me the following message.
$.ajax is not a function

The same using JQuery code like this:
var loadUrl = "visor_maple/ajax/updateimages.php";
jQuery(document).ready(function(){
jQuery.ajax({
type: "GET",
url: "ajax/updateimages.php",
data: "",
success: "",
});
});

Please, help me with that !
Thanks in advance,
bernat

Matt Thomson

unread,
Jan 19, 2010, 1:40:25 PM1/19/10
to Joomla! General Development
Here is how to do ajax with mootools. Jquery usually works on Joomla
pages, but I recommend you stick with mootools if possible, as it is
already loaded with the Joomla backend, and things get a bit messy
when someone uses motools and another developer uses jquery...

<?php
//tell joomla to make a head javascript link to media/systyem/js/
mootools.js
//Joomla will only do this once per request, even if it is called by 2
extensions
//This loads mootools 1.11, not mootools 1.2, hang in there, and
upgrade is likely soon
JHTML::_('behavior.mootools');
?>

//format=raw means joomla will just output the raw output of the
component, without any template stuff
<form name="myForm" id="myForm1" method="post" action="index.php?
option=com_myoption&amp;task=mytask&amp;format=raw" >
<input name="searchword" id="searchword1" maxlength="20"
class="inputbox" type="text" size="20" />
<input type="submit" name="submit" />
</form>

//if you don't need to dynamically change the headJs, put it in an
external file and use $document->addScript();
$headJs = '
window.addEvent(\'domready\', function()
{
$(\'myForm1\').addEvent(\'submit\', function(e)
{
//when the form submits, stop it from sending
new Event(e).stop();
$(\'myForm1\').send
({
//send the from with ajax
onComplete: function(response)
{
alert(response);
}
});
});';

//add the script to the head of the page
$document =& JFactory::getDocument();
$document->addScriptDeclaration($headJs);

In index.php?option=com_myOption&amp;task=myTask, do the php you want,
and echo the response, then instead of doing alert(response), you can
do something like:
$('messageDiv').setHTML(response);

There is more docs about this here:
http://docs111.mootools.net/Remote/Ajax.js#Element.send
http://docs111.mootools.net/Native/Element.js#Element.setHTML


On Jan 20, 6:42 am, bernat <bernat.puigdomen...@gmail.com> wrote:
> Ok guys,
>
> First of all, thank you for your replies. I tried a lot of things and
> I didn't succeed so I don't know what to do....
>

> I want to use Ajax on the Client part so I started withhttp://p2p.wrox.com/content/articles/adding-ajax-joomla-web-applications

Bill Engle

unread,
Jan 19, 2010, 2:20:56 PM1/19/10
to Joomla! General Development
You should put your straight PHP code into Joomla MVC. It's much
easier. The below task "yourajaxfunction" is the code from
ajaxfunction.php in MVC format.

JS example:
var url = 'index.php?
option=com_yourcomponent&controller=yourcontroller&task=yourajaxfunction';

var request = new Ajax(url, {
method: 'post',
update: some_div,
data: options,
onComplete: function (){
$('some_field').setHTML('I am finished!');
}
}).request();

Hope that helps.

-Bill

On Jan 19, 12:42 pm, bernat <bernat.puigdomen...@gmail.com> wrote:
> Ok guys,
>
> First of all, thank you for your replies. I tried a lot of things and
> I didn't succeed so I don't know what to do....
>

> I want to use Ajax on the Client part so I started withhttp://p2p.wrox.com/content/articles/adding-ajax-joomla-web-applications

Mahtab Darvishi

unread,
Nov 26, 2012, 8:32:24 AM11/26/12
to joomla-de...@googlegroups.com
Hi guys,
I have this problem.
I wanna  use Ajax in a plugin for a component, i dont know how to call the page to get response?

Allon Moritz

unread,
Nov 26, 2012, 8:35:30 AM11/26/12
to joomla-de...@googlegroups.com
If you are in joomla 3 use jQuery.ajax();...


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/mrep4IBZ8AIJ.

Mahtab Darvishi

unread,
Nov 26, 2012, 9:40:00 AM11/26/12
to joomla-de...@googlegroups.com
I`m using Joomla 2.5!

Michael Babker

unread,
Nov 26, 2012, 1:27:45 PM11/26/12
to joomla-de...@googlegroups.com
Look at the installation folder and admin com_finder to see how they use AJAX.

-Michael

Please pardon any errors, this message was sent from my iPhone.

On Nov 26, 2012, at 8:40 AM, Mahtab Darvishi <mahtabd...@gmail.com> wrote:

I`m using Joomla 2.5!

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/-8E13VpUmN4J.
Reply all
Reply to author
Forward
0 new messages