Click here to Skip to main content
14,770,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
		}
	}
}
}
Posted
Comments
Prava-MFS 22-Jul-15 1:52am
   
Have you added jQuery library file into your page? If not, then add this one for testing : <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>. If you already added, then check what value is coming for $_POST['usermsg'] in chat_new.php file.
Prava-MFS 22-Jul-15 1:56am
   
If you want to get idea of how serialize() works, then please go through this Link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900