Welcome Guest, Not a member yet? Register   Sign In
Ajax help
#1

[eluser]Cgull[/eluser]
Hello,

I've just started learning codeigniter and struggling with ajax calls.

I found a few tutorials but I can't understand how to do what I want.

I have a home_view with links, and when I click on a link I want it to fetch data from the database with the given id.

Then display a list of the records recieved from the database.

For example:

If the user clicks the Bread Rolls link, it should find all the records that belong to Bread Rolls in the products table and display them in the productsList div, in the categories_view file.

My jquery code:
Code:
[removed]
$(document).ready(function() {
$('.cat').click(function() {
  var catId = $(this).attr('id');

  $.get('/index.php/home/productsList'+catId, function(data){
   $(this).parents('div').append(data);
  })
});
});
[removed]

My categories view:
Code:
<div id="category">
<ul>
  &lt;?php
   foreach($cats as $cat)
   {
     echo '<li><a href="#" id="' . $cat['id'] . '" class="cat">' . $cat['name'] . "</a></li>";
   }
  ?&gt;
</ul>
</div>
<div class="clear"></div>
<div id="productList"></div>&lt;!-- Will be filled with an ajax call --&gt;

The home controller function:
Code:
function productsList()
{
    $this->load->model('m_products','', TRUE);
    $data['products'] = $this->m_products->getProducts($this->params['catId']);
    $this->load->view('productList', $data);
}

When I click on a category link, nothing happens. Can someone please help?




Theme © iAndrew 2016 - Forum software by © MyBB