Return to Snippet

Revision: 69443
at June 23, 2015 04:12 by olesla


Initial Code
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    header("Content-Type: text/html; charset=utf-8");
    session_start();
    require_once("functions.php");
    if ($_POST['action']=='update')
    {
        $result = $slaattene_db->query("SELECT * FROM chat ORDER BY time DESC LIMIT 10");
        $return = '<table>';
        while ($row = $result->fetch_assoc())
        {
            $message[]=$row;
        }
        $message=array_reverse($message);
        foreach($message as $msg)
        {
            if(substr($msg['message'], 0, 4 ) === "http" || substr( $msg['message'], 0, 3 ) === "www") // <a href="www.google.com">google.com</a>
            {
                $return .= '<tr><td><font color="#32cd32">' . substr($msg['time'], -8) . '</font></td><td><strong>' . $msg['handle'] . ':</strong></td><td> <a href="'.$msg['message'].'">'.$msg['message'].'</a></td></tr>';
            }
            else
            {
                $return .= '<tr><td><font color="#32cd32">' . substr($msg['time'], -8) . '</font></td><td><strong>' . $msg['handle'] . ':</strong></td><td> ' . $msg['message'] . '</td></tr>';
            }
        }
        $return .= '</table>';
        die($return);
    }
    if ($_POST['action']=='send')
    {
        $time = date("Y-m-d H:i:s");
        $handle=$_POST['handle'];
        $message=$_POST['message'];
        $slaattene_db->query("INSERT INTO chat (handle, message, time) VALUES ('$handle','$message','$time')");
        die();
    }
    if ($_POST['action']=='updateusers')
    {
        $handle=$_POST['handle'];
        $activeUsers=$slaattene_db->query("SELECT * FROM chat_current_users WHERE lastseen > NOW() - INTERVAL 2 MINUTE");
        $time = date("Y-m-d H:i:s");
        $slaattene_db->query("UPDATE chat_current_users SET lastseen='$time' WHERE handle='$handle'");
        $return='<table><tr><strong>Online:</strong></tr>';
        while($row=$activeUsers->fetch_assoc())
        {
            $return.='<tr><td>'.$row['handle'].'</td></tr>';
        }
        $return.='</table>';
        die($return);
    }
    if($_POST['action']=='sethandle')
    {
        $handle=$_POST['handle'];
        $checkHandle=$slaattene_db->query("SELECT * FROM chat_current_users WHERE handle='$handle'");
        if($checkHandle->num_rows == 0)
        {
            $time = date("Y-m-d H:i:s");
            $slaattene_db->query("INSERT INTO chat_current_users (handle, lastseen) VALUES ('$handle','$time')");
        }
        else
        {
            $slaattene_db->query("UPDATE chat_current_users SET lastseen='$time' WHERE handle='$handle'");
        }
    }
    die();
}
echo '<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script></head>';

if (!isset($_GET['p']))
{
echo '
<table width=1024>
        <tr>
            <td width=800><div id=chatwindow style="margin: 10px 0 10px 0;"></div></td><td valign="top"><div id=showusers style="margin: 10px 0 10px 0;"></div></td></tr>
        <tr>
            <td><input type="text" id="message" size=50 placeholder="message">
            <input type="button" value="Send" onclick="send_message();" />
            </td><td><input type="text" name="handle" id="handle" placeholder="Type a name"><input type="button" value="Set" onclick="set_handle();" /></td>
            </td>
            <td>
            </td>
        </tr>
</table>
    <script>
    function update_chat() {
        $.post("index.php", {
                action:"update"
            },
            function(data) {
                $("#chatwindow").html(data);
            }
        );
    }
    function update_users() {
        $.post("index.php", {
                action:"updateusers",
                handle:$("#handle").val()
            },
            function(data) {
                $("#showusers").html(data);
            }
        );
    }
    function set_handle()
    {
        $.post("index.php", {
                action:"sethandle",
                handle:$("#handle").val()
            },
            function(data) {
                $("#showusers").html(data);
            }
        );
    }
    function send_message() {
        if($("#handle").val())
        {
            $.post("index.php", {
                    action:"send",
                    handle:$("#handle").val(),
                    message:$("#message").val()
                },
                function(data) {
                    $("#message").val("");
                    update_chat();
                }
            );
        }
        else
        {
            alert("You need a handle before chatting!");
        }
    }
    $("#message").keydown(function(e){
        if (e.keyCode == 13)
        {
            send_message();
        }
    });
    setInterval(function(){
        update_chat();
        update_users();
    }, 3500);
    </script>';
}
?>

Initial URL

                                

Initial Description
durr

Initial Title
php + jquery chat

Initial Tags
php, jquery

Initial Language
jQuery