0

I want to make a chat from scratch and I'm stuck trying to load div using ajax, but when I do that, my whole page shows inside that div.

<script>
    function auto_load(){
        $.ajax({
            url: "chat.php",
            cache: false,
            success: function(data){
                $("#chatbox").html(data);
            } 
        });
    }

    $(document).ready(function(){
        auto_load();
    });
    setInterval(auto_load,2000);
</script>
ekad
  • 14,436
  • 26
  • 44
  • 46
Monk
  • 105
  • 9
  • 1
    What does `chat.php` output? – showdev Sep 20 '16 at 16:36
  • @showdev the same page where that code is. Maybe i should create another page and put the code that i want to show? – Monk Sep 20 '16 at 16:38
  • 2
    It depends on how you've constructing things, but I'd suggest calling a PHP script that only returns chat data to be displayed. As you have it now, whatever HTML is returned by `chat.php` will be displayed in the `#chatbox`. – showdev Sep 20 '16 at 16:41
  • @showdev ok i've made it work...thank you – Monk Sep 20 '16 at 16:42
  • Ok great. If you'd like, post an answer of how you solved it. You also might find [websockets](http://stackoverflow.com/questions/3682198/writing-a-chat-application) interesting. – showdev Sep 20 '16 at 16:45
  • @showdev Ok thank you. Do you know how to fix when i submit message to not scroll to top, so when i submit message, it sends me to the top of the page. – Monk Sep 20 '16 at 16:56
  • And yeah, i created load.php page and changed `url: "chat.php"` to load.php page and put the chat showing code inside of it. – Monk Sep 20 '16 at 16:58
  • That might be better as a [new question](http://stackoverflow.com/questions/ask). One idea is to submit data with AJAX so as not to reload the page. – showdev Sep 20 '16 at 17:00

0 Answers0