How to update database query using jQuery

I am working on a real basic chat that is mostly php. I would like to use jQuery to check for new posts and update the #cbox

div every few seconds. So far, I've tried to do it using the code below. For a frame of reference, this is my first time including jQuery in any of my scripts, so the problem is probably a very simple error that I ignore in my newbie.

The problem I am facing is the div #cbox

is empty, it doesn't seem to ring at all chat_post.php

. I will also post this code if the problem is with this and not my jQuery. To fix the error, I also tried including <div id='cbox'>

in chat_post.php

, but gave no results. At this point, I'm grabbing at straws quite a bit.

<div id='sb_content'>
    <center><div id='chat'>

        <div id='ribbon' style='position: relative; margin-top:-50px; margin-left:-32px;'><center><span class='ribbon'>chat box</span></center></div>
        <div style='margin-top: -20px;'><center><span style='text-shadow: 0 0 0.2em #000;' class='admin'>admin</span> || <span style='text-shadow: 0 0 0.2em #000;' class='mod'>mod</span></center></div>

        <div id="cbox">
        <script>
        $(document).ready(function() {
            setInterval(function(){getUpdates()}, 2000); 
        });

        function getUpdates() {
            $("#cbox").load("chat_post.php");
        }
        </script>
        </div>

        <form name="message" method='post' action="chat_post.php" id="cbox_input">
            <input name="usermsg"id='usermsg' type="text" size="63" maxlength="255" />
        </form>

    </div></center>
    </div>

      

chat_post.php

<div id='cbox' align='left'>

<?php

$username = $_SESSION['login'];
$ug = $_SESSION['user_group'];


// Display messages
$sql = <<<SQL
SELECT *
FROM `chat_entries`
ORDER BY `ts` DESC
LIMIT 0,50
SQL;

$result = $db->query($sql);
$count = $result->num_rows;

if ($count != 0){
    while ($row = mysqli_fetch_array($result)) {
            $c_name = $row['author'];
            $c_text = $row['text'];
            $c_ts = $row['ts'];
            $c_id = $row['id'];

$sqlm = <<<SQL
SELECT *
FROM `users`
WHERE username = '$c_name'
SQL;

            $resultm = $db->query($sqlm);
            $countm = $resultm->num_rows;

            if ($count != 0){
                while ($rowm = mysqli_fetch_array($resultm)) {
                    $c_level = $rowm['user_group'];
                }
            }

            if ($c_level == 'mod') {
                echo "
                <a href='/user/" . $c_name . "' class='mod'>" . $c_name . "</a>
                ";
            }

            if ($c_level == 'admin') {
                echo "
                <a href='/user/" . $c_name . "' class='admin'>" . $c_name . "</a>
                ";
            }

            if ($c_level == 'member') {
                echo "
                <a href='/user/" . $c_name . "' class='member'>" . $c_name . "</a>
                ";
            }

            if ($c_level == 'guest') {
                echo "
                <i>" . $c_name . "</i>
                ";
            }

            echo "
            : " . $c_text . "<div id='cbox_div'>";

            echo "<span style='float:left;'><i>" . $c_ts . "</i></span>";

            echo "<span style='float:right;'>";

            if ($ug == 'mod' || $ug == 'admin'){
                echo " <a href='#'>Delete</a> || <a href='#'>Block</a> ||";
            }

            if ($_SESSION['login']) {
                echo "<a href=/report/?type=chat?id=" . $c_id . ">Report</a></span></div>
                ";
            }
    }
}
?>

      

+3


source to share


1 answer


After removing the extra id, chat_post.php

my code worked fine. It's funny that something so simple can really ruin things. (: Thanks everyone for your help!



0


source







All Articles