Plummer
Plummer

Reputation: 6688

Loading component data in Joomla with Ajax

I'm switching over my component to use AJAX to make for prettier/easier/faster/better pagination and content loads.

I'm trying to get past the first part which is simply displaying the content from a mySQL/PHP query.

Since I already had the query built, I figure why re-invent the wheel? ...but it would help if I knew what a circle looked like, so to speak.

Here is the basic AJAX/jQuery function I'm trying to use:

$.ajaxSetup ({  
    cache: false  
});  

var loadUrl = "http://www.mgoode.com/index.php";  

$("#load_basic").click(function(){  
   $("#result").load(loadUrl, "option=com_mls&view=list&lprice=100000&pstart=5&plimit=5");  
});

I'm hoping that in the future, when I implement pagination, I can just alter the variables in the URL arguments and not have to re-do a bunch of stuff.

I have a JSFiddle HERE. I would appreciate any advice.

Thanks in advance!

EDIT: So, I just remembered that all of my element creations happen in the template itself. So, no wonder the JSFiddle will only show up blank. Sorry to bother you guys.

Upvotes: 0

Views: 3798

Answers (1)

vorillaz
vorillaz

Reputation: 6276

Using Joomla with AJAX is a beat tricky but works fine if you structure your files right

Since you can't provide us with your PHP I'll pass you a recent modification I had made. My goal was to use com_mycomponent within a module and retrieve some data from the db

My default.php for the module (modules/mod_mymodule/tmpl/default.php) looks like this

<form id="myform" name="myform">
<label>Push this to load some data</label>
<input type="text" id="myid" name="myid"/>
<a href="#" id="pushme">Push me</a>
<div id="component data"></div>
</form>?

It's a pretty form to grab an ID to make a simple SQL query

On my component I had to modify controller.php (component/com_mycomponent/controller.php)

<?php
defined('_JEXEC') or die;
class MyComponentController extends JControllerLegacy
{ 
public function myFunction()
    {
         $id = JRequest::getString('myid', '', 'method', JREQUEST_ALLOWRAW);
         //grab the id
         $db->setQuery('SELECT username FROM #__mytable WHERE id='.$db->Quote($id));
         //fetch data
         $json['returned'] = $result->name;
     echo(json_encode($json));
        }
}
?>

The code above takes the posted ID makes a query and returns some data Least but not last my jQuery function

$('a#pushme').click(function(){ 
$.ajax({
        url: 'index.php?option=com_users&format=raw&task=loginme',
        data: dataString,
        dataType: 'json',
        type: 'POST',
        success: function(data) {

       if(data!=null)   {
});

Upvotes: 1

Related Questions