So I am working on a php/jquery powered chat. Most of the features work, but when I try to submit a message, the jQuery doesn't seem to properly connect with the PHP and nothing happens. The PHP script works fine on it's own when I removed jQuery from the picture, so I'm certain it's the jQuery that's messing things up.
I hardcoded a value into
data:
and the messages would appear fine (with every message being that value, as expected). It's when I replace it with
$("#cbox_input").serialize(),
that things stopped posting.
Here is the code used in the chat.
<div id="cbox" align="left">
<script>
$(document).ready(function() {
setInterval(function(){getUpdates()}, 2000);
function getUpdates() {
$("#cbox").load("/lib/chat_post.php");
}
$("#submitmsg").click(function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: '/lib/chat_new.php',
data: $("#cbox_input").serialize(),
success: function(){getUpdates()}
});
});
});
</script>
</div>
<form name="cbox_input" method='post' id="cbox_input">
<input name="usermsg" id='usermsg' type="text" size="63" maxlength="255" />
<input name="submitmsg" id='submitmsg' type="submit" />
</form>
And here is
chat_new.php
in case it is something there that is throwing a wrench in things.
ob_start();
session_start();
require "/home3/himowa/public_html/life/inc/functions.php";
$username = $_SESSION['login'];
$ug = $_SESSION['user_group'];
if ($_POST['submitmsg']){
if ($username) {
$text = $_POST['usermsg'];
$text = stripslashes($text);
$text = mysqli_real_escape_string($db,$text);
if ($text) {
$sql = <<<SQL
INSERT INTO chat_entries(author,text)
values('$username','$text')
SQL;
$result = $db->query($sql);
}
}
if (!$username) {
$ip = $_SERVER['REMOTE_ADDR'];
$sqlm = <<<SQL
SELECT *
FROM `chat_guests`
WHERE ip = '$ip'
SQL;
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if($countm == 0) {
$sqlg = <<<SQL
INSERT INTO chat_guests(ip)
values('$ip')
SQL;
$resultg = $db->query($sqlg);
}
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if($countm == 1) {
while ($rowm = mysqli_fetch_array($resultm)) {
$gid = $rowm['id'];
}
$username = "Guest" . $gid;
$text = $_POST['usermsg'];
$text = stripslashes($text);
$text = mysqli_real_escape_string($db,$text);
if ($text) {
$sql = <<<SQL
INSERT INTO chat_entries(author,text)
values('$username','$text')
SQL;
$result = $db->query($sql);
}
}
}
}