DumDumDummy
DumDumDummy

Reputation: 81

ajax auto refresh div values other version

My case is different to other questions. I have a div with this code <div id="ondiv"><?php ?></div>

Inside that php are people who are online. When a user's login the div will refresh and be able to see the person who just logged in. I have tried the code from previous question like this.

$(document).ready(function(){
setInterval(function(){
$("#oldiv").load("chat.php");
}, 2000);
});

`but what it did is reload the page and what it display on my div was the whole page of the chat.php. What I want is just refresh the div's to display the user who just login inside php code.

Upvotes: 0

Views: 1127

Answers (3)

Than Ngo Hoai
Than Ngo Hoai

Reputation: 469

Refer to load method of jQuery: http://api.jquery.com/load/

It takes the content of chat.php and place inside #oldiv.

You can change your chat.php or create a new file to achieve what you need.

Upvotes: 0

Jurij Jazdanov
Jurij Jazdanov

Reputation: 1268

.load() really only usable when the call only will result in HTML. If I were you I would do this with ajax:

$(document).ready(function(){
    setInterval(function(){
        $.ajax({
            type: 'post',
            url: 'chat.php',
            success: function(data) {
                $("#oldiv").html(data); // or ondiv, didn't get it
            }
        });
    }, 2000);
});

Upvotes: 1

Rayon
Rayon

Reputation: 36609

I assume you have a container(div) in chat.php which holds all the online users.

You can load only fragment from the page in your target element.

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. Ref this

Try this:

$(document).ready(function() {
  setInterval(function() {
    $("#oldiv").load("chat.php #container_which_holds_online_user");
  }, 2000);
});

Upvotes: 0

Related Questions