Welcome Guest, Not a member yet? Register   Sign In
Passing variables to Controller ?
#1

[eluser]yannyannyann[/eluser]
Hi there Smile

I have an ajax call to make and I want to pass variables my controler

www.mysite.com/controller/index/

Code:
function index($offset = 0) {
...
//here i call the model that does a DB query based on the $offset
...
}


So for example with jQuery how do I pass the arguments ?
Is it something like this ?
Code:
$.ajax({
                type: "POST",
                url: "www.mysite.com/controller/index/",
                data: "?offset=1",
                success: function(msg){
                    // ...
                }
            });
#2

[eluser]xwero[/eluser]
Code:
$.ajax({
                type: "GET",
                url: "www.mysite.com/controller/index/1",
                success: function(msg){
                    // ...
                }
            });
#3

[eluser]yannyannyann[/eluser]
Ok thanks, it works.

My link to call the ajax is down the page and I don't want the page to go up again once I click and do the ajax call. I guess it has something to do with the HREF="#", but I don't remember how to solve it ?

Code:
<li><a href="#" id="next">Next</a></li>

Code:
[removed]
        $(function() {
            $('#next').click(function() {
                $.ajax({
                    type: 'GET',
                    url: 'www.mysite.com/controller/index/1',
                    data: '',
                    success: function() {
                        console.log("cool") ;
                    }
               });
            });
        });
        [removed]
#4

[eluser]yannyannyann[/eluser]
Ok I removed the href="#" ...
#5

[eluser]yannyannyann[/eluser]
I'd like to know now how I can pass the arguments :

- current page
- current language

I have in PHP this function ready :
Code:
current_language() // returns "fr" or "en"


For example I would like to be able to pass these parametres to the URL like in the below examples, but dynamically :

Code:
$.ajax({
   type: 'GET',
   url: 'www.mysite.com/photo/dataTable/1',
   data: "",
   success: function(msg) {                        
       $('#ajaxlist').empty().append(msg);
    }
});

Code:
$.ajax({
   type: 'GET',
   url: 'www.mysite.com/en/photo/dataTable/2',
   data: "",
   success: function(msg) {                        
       $('#ajaxlist').empty().append(msg);
    }
});

Code:
$.ajax({
   type: 'GET',
   url: 'www.mysite.com/photo/dataTable/3',
   data: "",
   success: function(msg) {                        
       $('#ajaxlist').empty().append(msg);
    }
});
#6

[eluser]yannyannyann[/eluser]
To sum up,

is it possible to do something like this in my jQuery Ajax call?
(check out the BASE_URL call and the $current_block variable )


Code:
$('#next').click(function() {
                $.ajax({
                    type: 'GET',
                    url: '&lt;?=base_url()?&gt;photo/index/dataBlock/&lt;?=$current_block?&gt;',
                    data: "",
                    success: function(msg) {                        
                        $('#dataBlock').empty().append(msg);
                    }
               });
});
#7

[eluser]n0xie[/eluser]
I think you need to solve that with javascript.

Seems to me you want to simulate paging. In that case store the current page in a javascript variable. Add 1 to this variable whenever someone presses '#next', and send the ajax request.

Something like this (this is untested pseudo code so it might not work):

Code:
var currentpage = &lt;?=$current_block?&gt;;

$('#next').click(function() {
  currentpage = currentpage +1;
                $.ajax({
                    type: 'GET',
                    url: '&lt;?=base_url()?&gt;photo/index/dataBlock/'+currentpage,
                    data: "",
                    success: function(msg) {                        
                        $('#dataBlock').empty().append(msg);
                    }
               });
});
#8

[eluser]yannyannyann[/eluser]
yes it's a kind of paging system, you are right.

Thanks I will declare this currentpage in JS.




Theme © iAndrew 2016 - Forum software by © MyBB