Hi, I have this code here:

index.php

<?php 
include 'core/init.php';
include 'includes/overall/overall_header.php'; 
?>
<h1>Welcome to AwsomeChat!</h1>
<p>This is AwsomeChat - a free place just to hang out. This site is made for chatting or socializing with a friend. This site is free, no membership needed to use this site.</p>
<?php
if(logged_in() === true) {
?>
    <script type="text/javascript">
    $(document).ready(function() {
    $("#messages").load('chat/feed.php');

    $("#chat").submit(function() {

        $.post('chat/ajaxPost.php', $('#chat').serialize(), function(data) {
            $("#message").val("");
            timeout(90000);
        });
        return false;
    });
});
    setInterval(function () {
        $("#messages").load('chat/feed.php');
    }, 1000);

    </script>
    <h1>Home</h1>
    <h3>Welcome, <?php echo $user_data['username']; ?>!</h3>

    <!--Messages form-->
    <form id="chat">

        <input type="text" maxlength="200" name="messages" id="message" placeholder="Type your message here." /><br>
        <input type="submit" value="Send" />

    </form>

    <!--Display the messages here-->
    <div id="messages"></div>

<?php
}
include 'includes/overall/overall_footer.php'; 
?>

ajaxPost.php

<?php
ob_start();
include 'config.php';
include '../core/init.php';

mysql_connect('localhost', 'root', '');
mysql_select_db('DB');

$v = trim($_POST['messages']);
if(logged_in() === false){
    // Redirect to the index.php page
    header('Location: http://www.url.tk/test/');
    exit();
} else if(empty($v)){

} else {
    $message = mysql_real_escape_string(htmlentities($_POST['messages']));
    $username = $user_data['username'];
    $DB->Query("INSERT INTO messages(`username`, `message`, `time`) VALUES('$username', '$message', UNIX_TIMESTAMP())");
}
echo $message;
ob_flush();
?>

feed.php

<?php
include 'config.php';
include '../core/init.php';
$DB->Query('SELECT * FROM `messages` ORDER BY  `messages`.`id` DESC LIMIT 0, 10');
$data = $DB->Get();
foreach($data as $key => $value) {
    $profile_image = user_profile_from_username($value['username']);
    $postid = $value['id'];
    $username = $value['username'];
    $status = user_online($username);
    $text = autolink($value['message']);
    // Is the user online?
    if ($status === false){
        //The user is offline
        $online = "Offline";
        $style = "color:red;";
    } else {
        //The user is online
        $online = "Online";
        $style = "color:green;";
    }
    echo "<table style='background-color:lightblue; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-left-radius: 10px; border-bottom-right-radius: 10px; '>" . "<tr>" . "<td>" . "<a href='$value[username]'><img src='$profile_image' width='120' height='120' /></a>" . "</td>" . "<td>" . "<a href='$value[username]'>$value[username]</a> &nbsp; &nbsp; <b style='$style'>$online</b>" . "<br /><br />" . "<div style='width:400px; word-wrap:break-word; overflow:auto;'><b>" . $text . "</b>" . "</div>" . "<br />" . "<a href='http://www.url.tk/report.php?ref=chat_message&username=$username'>Report</a>" . "&nbsp;&nbsp;&nbsp;" . "<a href='http://www.url.tk/test/chat/delete.php?id=" . $value['id'] . "'>Delete Post</a>" . "</td>" . "</tr>" . "</table>" . "<br />";
}
?>

This code works, I just need to know where to put a wait function so that the user cannot post more than one post per 2 seconds.

Thanks.

Member Avatar for diafol

How about disabling the button? Here's some jQuery I cobbled together:

$(function() {
    $('#mybutton').click(function(){
        $('#mybutton').attr("disabled", "disabled");
        setTimeout(enableMe, 2000);
    });

    function enableMe(){
       $('#mybutton').removeAttr('disabled');
    }
});

That just goes in your <script> tag

I don't think it would stop a guy with multiple tabs of your chat though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.