0

I am able to display the text straight from database on a page. The thing is that when the text is edited in database, I want it to instantly reload and display the new text without reloading the page.

I am not able to make that happen and I don't know why it's not wokring. I want it to reload every 2 seconds.

My view file

<div id="results"></div>

<script>
        $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html);
            },
           Timeout:2000
    });
</script>

My controller

 public function get() {

    $this->load->model('editor_model');
    $result = $this->editor_model->get();
    if ($result) {
        echo $result->body;

    }
}

It displays it but doesn't reload. Please help. Thanks!

2
  • alert(html) in success, check if its getting expected result... Commented Oct 31, 2015 at 18:14
  • Is there an event that is triggered before the text is sent for update ?? Like click, hover etc .. Commented Oct 31, 2015 at 18:20

2 Answers 2

1

Timeout is wrong to use like that

Try like this,

var i = setInterval(function(){
     $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html);
            },
    });
},2000)
Sign up to request clarification or add additional context in comments.

3 Comments

Nothing is displaying. It's blank. If I remove ` function get_data(){` it displays but doesn't display if it's there. With he first one, it doesn't display at all. Please help.
Edit: fixed it, I had an extra }. Thank you!
Glad it helped you. COnsider accepting and upvoting the answer.
1

you need to use setInterval()

<script>
 $(document).ready(function(){
   var interval = function(){
        $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html); // you can use .append(html) instead of .text(html) to append new data to div
            },
           Timeout:2000
       });
    });
    //var get_new_data = setInterval(interval , 5000); 
    setInterval(interval , 5000);// it will refresh ajax every 5 seconds you can use a previous line if you need to stop interval by using clearInterval(get_new_data);
});
</script>

2 Comments

Thanks for your reply. The page is displaying a blank page. Nothing is being shown
Yes, there is an extra } at the end. I fixed it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.