Chatbox White Space!

Fait

Seasoned Veteran
Joined
Oct 15, 2010
Messages
4,407
Reaction score
561
FP$
2,054
I have created a chatbox There is one small bug that I need help in fixing,

When I post a new message it duplicates the values so if a user submits it 2 times without refreshing the page it displays all errors double. Heres a image of what I mean 


http://tinypic.com/view.php?pic=2ewlkww&s=6


How could this be fixed without changing the code to much? This is also causing White Space thats not needed.

Thanks, 

Spudster

-- December 23rd, 2012, 3:46 pm --

Heres the code
insert.php

Code:
 <?php
 session_start(); 
 require "core/error_check.php";
 $dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'chat';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

echo "Testing"; 
if(isset($_POST['content']))


$content= nl2br(htmlentities((trim($_POST['content']))));
$session = $_SESSION['username'];


if(empty($content)){

echo '<script type="text/javascript">alert("' . 'You Must Include A Message' . '"); </script>';
die;

}

mysql_query("insert into chatbox(username, message) values ('$session', '$content')");

?>


<table cellpadding="0" cellspacing="0" width="500px">

<tr class="comment">
<td style="padding:14px;" class="comment_box" align="left"><b>




</b></td>
</tr>

</table>
[/code:]

Java/Fom
[code:]




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Insert Record with jQuery and Ajax</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
 <script type="text/javascript">
 
 


 
$(function() {

$(".comment_button").click(function() {


var element = $(this);
   
    var content = $("#content").val();
	
    var dataString = 'content='+ content;
	
	
	
	if(content=='')
	{
	
	
	}
	else
	{
	$("#flash").show();
	$("#flash").fadeIn(400).html('<img src="http://tiggin.com/ajax-loader.gf" align="absmiddle">&nbsp;<span class="loading">There Apears To Be A Probablem Sending.</span>');
	
		
		$.ajax({
		type: "POST",
  url: "demo_insert.php",
   data: dataString,
  cache: false,
  success: function(html){
  
  
  
    $("#display").after(html);

 document.getElementById('content').value='';
 $("#flash").hide();
	
  }
  
  
});
	}
		

    return false;
	});



});
</script>






</head>

<body>

    

<form  method="post" name="form" action="">

<td class="comment_box">
<textarea cols="30" rows="2" name="content" id="content" maxlength="145" 

onkeydown=" 




";


"></textarea><br />
<input type="submit"  value="Send"  id="v" name="submit" class="comment_button"/>
</td>

</tr>

</table>
</form>

</div>
<div style="height:7px"></div>
<div id="flash" align="left"  ></div>

<div id="display" align="left"></div>

</td>
</tr>
</table>

</div>
</body>
</html>
 
I'd recommend just removing the status message (for ex: Testing, or loading).
The user will know when their message shows up as they'll see it in the Chatbox.
I would honestly just remove it 🙂

You could fade the Send button between messages to prevent Chatbox flood.
 
Back
Top Bottom