chat session in chatbox in php

//Updates the chat
function updateChat(){
  $.ajax({

    type: "GET",
    url: "update.php",
    data: {  
        'state': state,
        'file' : file
        },
    dataType: "json",
    cache: false,
    success: function(data) {

        if (data.text != null) {
            for (var i = 0; i < data.text.length; i++) {  
            $('#chat-area').append($("<p>"+ data.text[i] +"</p>"));
        }

        document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;

    }  

    instanse = false;
    state = data.state;
    setTimeout('updateChat()', 1);

    },
  });
}

4
6
Festerfaster 125 points

                                    var lastTimeID = 0;

$(document).ready(function() {
&nbsp;&nbsp;$('#btnSend').click( function() {
&nbsp;&nbsp;&nbsp;&nbsp;sendChatText();
&nbsp;&nbsp;&nbsp;&nbsp;$('#chatInput').val(&quot;&quot;);
&nbsp;&nbsp;});
&nbsp;&nbsp;startChat();
});

function startChat(){
&nbsp;&nbsp;setInterval( function() { getChatText(); }, 2000);
}

function getChatText() {
&nbsp;&nbsp;$.ajax({
&nbsp;&nbsp;&nbsp;&nbsp;type: &quot;GET&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;url: &quot;/refresh.php?lastTimeID=&quot; + lastTimeID
&nbsp;&nbsp;}).done( function( data )
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;var jsonData = JSON.parse(data);
&nbsp;&nbsp;&nbsp;&nbsp;var jsonLength = jsonData.results.length;
&nbsp;&nbsp;&nbsp;&nbsp;var html = &quot;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;for (var i = 0; i &lt; jsonLength; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var result = jsonData.results[i];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html += '&lt;div style=&quot;color:#' + result.color + '&quot;&gt;(' + result.chattime + ') &lt;b&gt;' + result.usrname +'&lt;/b&gt;: ' + result.chattext + '&lt;/div&gt;';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastTimeID = result.id;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;$('#view_ajax').append(html);
&nbsp;&nbsp;});
}

function sendChatText(){
&nbsp;&nbsp;var chatInput = $('#chatInput').val();
&nbsp;&nbsp;if(chatInput != &quot;&quot;){
&nbsp;&nbsp;&nbsp;&nbsp;$.ajax({
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: &quot;GET&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: &quot;/submit.php?chattext=&quot; + encodeURIComponent( chatInput )
&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;}
}

4 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to make chat box in php php chat sessions create a php live chat without no load php chat creating a web chat with php and mysql create chat php chat php llocalhost\mehra1\chat.php php chat rooms easy live chat php jQuery chat box example chatting with ajax chat conversation app in php php chatroom simple chat page in php create live chat using php how to create live chat support in php chat system in php chat screen using php PHP Projects - PHP Chat Application Tutorial 01 - Complete PHP Chat System - PHP Group Chat how to update the seconds users chat if user sends message chat app php how to make chat function in php how to create a chat system inbetween two uers in pho how tomak an ajax live chat ajax live chat app how to make chat website in php chat room php chat app mit php chat application with php and mysql chat applciation with php php chat room real time chat jquery popup realtime chat box using php php chat room script php private chatroo how to create chat using php how to let users know when a new message is sent in a chat application in php live chat system in php web chat tutorial chat room php mysql whatsapp like private chat history in php best way to make a chat php 2020 best way to make a chat php client chat/bot server/client for website jquery simple chat server/client for website jquery html chat room how to make chatting website in php php live chatting jquery messages chat css php chat system 2020 tutorial on how to create ajax and php chat create a chat with php chat snippet for php how ro make a chat room php make chat on homepage with php building chatroom with javasript and php how to maintain chatid in chatboz php chat session in chatbox in php ajax chat room code chat room with php chat box php logic how to create a chatroom using php php chat function how to create a live chat application in php making a chat site using php jquery live chat live chat php html simple php chat application for website create live chat in php website how to make a chat website with php build a chat with js and php
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source