Welcome Guest, Not a member yet? Register   Sign In
Ajax + Database content
#1

[eluser]stuffradio[/eluser]
Has anyone done one of those AJAX tab things where you click a link and it switches the content in a div? If so, how did you do it where you switched it and when you switch it there are different results from the db using models?

I'm basically asking how I should pass results from models so I can load them in the div tag with ajax Big Grin
#2

[eluser]WebsiteDuck[/eluser]
If you're using a JS user interface framework like jQuery UI, they have a tab widget that does ajax automatically.

If you're using jQuery but not jQuery UI, you'll have to set up a callback like
Code:
$(".my-tabs").click( function() {
  $.post("<?=base_url()?>home/get_tab",
  {
    tab: $(this).attr("id") //or attr("title")
  },
  function(data){
    $("#tab-div").html(data);
  });
});
You may have to adjust the code, I didn't test it. Give your tabs the class "my-tabs"

And then you would create a get_tab method in your controller that interfaces with the model and then either echos the data out or calls a view.
The tab id will be available in $this->input->post('tab')

If you're not using jQuery then you'll have to do the ajax stuff yourself, and set up a function thats called in the onclick of your tabs, which will be a ton of fun.
#3

[eluser]vitoco[/eluser]
The model only retrieve the data from the storage device ( mysql database, text files ) to the controller...and this one, handle the data to retrieve it to the client browser throught view.

So, to load content ( a DIV ) with ajax, you must call a controller/method like this

http://domain.com/controller/method/param1/.../param2

Inside the controller/method, you must load the model to retrieve the data , and then send it to the client browser in a view.
Code:
...
function ajax_data()
{
    // LOAD MODEL
    $this->load->model('mdata');
    // GET PARAMS FRON URI
    $param1 = $this->uri->segment( 3 );
    ...
    $paramN = $this->uri->segment( $N + 3 );
    // GET DATA
    $data = $this->mdata->get_data( $param1 , .... , $paramN );
    // SEND DATA THROUGHT A VIEW
    $this->load->view( 'view.php' , $data  )
  
}
..

This is the server side, the client side was explained before with an jquery example.
#4

[eluser]stuffradio[/eluser]
I know the model only retrieves data, I already know about models and databases and fetching data and how to pass it to a view, I was only wanting to know about AJAX and getting data with AJAX.

@WebsiteDuck I'll try it later, thanks Smile
#5

[eluser]vitoco[/eluser]
you have troubles with the server or client side??
#6

[eluser]stuffradio[/eluser]
Haven't tried WebsiteDuck's solution yet, I will let you know if I still need help Smile
#7

[eluser]vickel[/eluser]
@ websiteduck

change the line

Code:
$.post("<?=base_url()?>home/get_tab",

to

Code:
$.post("<?=site_url()?>/home/get_tab",




Theme © iAndrew 2016 - Forum software by © MyBB