1

hi i am working on a codeigniter project. I want to execute a php function after an interval of 10seconds. When a user visits that specific page after 10 seconds i want that php function to be executed. In that php function i have set a counter which adds 1 to the specific table into the database. I have tried using AJAX but didnt get the desired result. Kindly explain me with examples as i am new in ajax. Thanks in advance...

  • Are you sure there's no better way to do this? What are you trying to do? You might want to read [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – h2ooooooo Dec 23 '13 at 09:58
  • You can reference this http://stackoverflow.com/questions/3138756/jquery-repeat-function-every-60-seconds – lighter Dec 23 '13 at 09:59
  • @h2ooooooo I think he just want to roughly measure time people spend on the pages. – zavg Dec 23 '13 at 10:03
  • @h2oooooo actually i am working on a movie site and i am calculating the user response the user is directed to the playing movie page so i want the counter after 10 seconds when user is directed to that page instead of calculating it on click –  Dec 23 '13 at 10:08

4 Answers4

0

try this

setTimeout(function(){
    $.ajax({
    url: "<?php echo base_url('your/controller/address');?>",
   type: 'post',
   data: {"token": "your_token"},
});

}, 10000);

Example

in your view

$(document).ready(function(){

setTimeout(function(){
    $.ajax({
    url: "<?php echo base_url('MyController/Mymethod');?>",
   type: 'post',
   data: {"token": "12majid18"},
 });

 }, 10000);

});

and in Your Controller write method like this

public function Mymethod()
{
   $token = $this->input->post('token');

   if ( $token == '12majid18' )
     {
        /*call your model and insert your data in Table*/
     }
 }
Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
0

@Majid Golshadi s answer is the right answer.

Working version is here

Please in view that is loaded all the time (like header_view.php)

add this few lines

<script type="text/javascript">
        var _baseUrl = "<?= base_url() ?>";
</script>

This makes that your base_url is usable in JavaScript anywhere in page (but make sure to have it somewhere on "TOP" of page)

and literraly use @Majid Golshadi s answer in this way

$(document).ready(function() {
    setTimeout(function() {
        $.ajax({
        url: _baseUrl + "/your/controller/param",    
        type: 'post',    
        data: {"token": "your_token"}, });   
    }, 10000);
});
Kyslik
  • 8,217
  • 5
  • 54
  • 87
0

using jquery this will be the easiest and fastest way to do that

`//setting timeout to 3 seconds = 3 thousand milli seconds

 setInterval(function(){
     //ajax call
      $.ajax({
         url: _baseUrl + "/controller_name/function_name/",   //the url where you want to fetch the data 
         type: 'post', //type of request POST or GET    
         data: {"data": "value"}, }); //data passed to controller
 },3000);`

in your controller you may use

function function_name(){
    $var = $this->input->post();//getting data passed from ajax
    //process here...
    echo json_encode($var)//parses and returns the processed value
 }
Emil Reña Enriquez
  • 2,929
  • 1
  • 29
  • 32
0

you can try this:

window.jQuery(function(){
var y=setInterval(function() {
        window.jQuery.post(url,{"token": "your_token"},function(res){
        alert(res);
   });   
 }, 10000);
});
Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29