The original application code of ajax in joomla

  • 2020-05-19 04:22:18
  • OfStack

Today, the author is here to talk about using joomla mootools javascript library to achieve ajax. Since joomla is a built-in library, we don't need to worry about whether jquery is available or not. So how to achieve ajax?
 
<input type="button" id="ajaxButton" value="Get way " /> 
<div id="someID"></div> 
$doc =& JFactory::getDocument(); 
$script = <<<SCRIPT 
window.addEvent('domready', function() { 
$('ajaxButton').addEvent('click', function () { 
new Ajax( 
'index.php?option=com_category&controller=category&task=aj&format=ajax', 
{ 
method: 'get', 
update: 'someID' 
} 
).request(); 
}); 
}); 
SCRIPT; 

input this line 1 is a ajax button, then the display data returned div layer, is to use ajax mootools code below, and simply said, here is the method getDocument class head js is added to the document, and note the $represent mootools class libraries, ajax url parameter is first component name, controller name, aj execution method, this is written in the controller, is the final execution method, need to write return data, the last is the return data type, there will be It must be ajax, otherwise the whole document will be returned, we only need the return data part of aj method here. There are two parameters,get is the way to get data, and someID is the bound return data to show id, which are both necessary.

ajax use is not a must, but sometimes have to use, if you use the original, I want to load or soon, mootools library, the author also used, only with difficulty, this is, after all, provides one kind of realization method of the ajax, how realize POST method, you just need to add the corresponding form elements. The author here is not much said, because not many. Well, that's the end, will not provide sample, if you need help with editing form elements when try what the code is very short, copy1 next!

Related articles: