peacehunter Posted November 7, 2010 Share Posted November 7, 2010 Recently I am trying to make a chat system using jquery and php.....Its a very very simple chat system. But its not good enough.Coz when I load that chat system on my localhost , my pc become slow. Sometime it hangs. I have downloaded some pre made chat system , that was ok.After observed those code I saw that those code was too complex..Please tell me what have I done wrong,and give me an idea to make it better...my code is below ... (OHHHH.....I have forgotten to say that I am new in jquery).... index.html <html><body><center><form action="chat.php" method="post"><h1>Enter your name :</h1><input type="text" name="name" /><input type="submit" value="Submit" /></center></form></body></html> chat.php <?php$name=$_POST['name'];$text="<input type='hidden' value='$name' name='hidden' id='name'/>"; if($name==""){ header("LOCATION:index.htm"); } echo $text;?><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">function on_click(){$(document).ready(function(){var chat=document.getElementById("text").value;var name=document.getElementById("name").value; $.post("insert.php",{"hidden":name,"text":chat}); })}function msg(){$("#msg").load("show_msg.php");}setInterval(msg(),1000)</script><div id="msg"></div><input name="text" type="text" size="40" id="text"/><button id="button" onclick="on_click()">Submit</button> show_msg.php <?php$con=mysql_connect("localhost","root","") or die ("Could not connect");mysql_select_db("chat",$con) or die("Could not select");$query=mysql_query("SELECT *FROM chat_tbl ORDER BY id ") or die("Could not query");while($row=mysql_fetch_array($query)){ $id=$row['id']; $name=htmlspecialchars($row['name'])."</name>"; $msg=htmlspecialchars($row['msg']); echo $id.".".$name." says:".$msg."<br/>";}?> insert.php <?php$con=mysql_connect("localhost","root","") or die ("Could not connect");mysql_select_db("chat",$con) or die("Could not select");$name=$_POST['hidden'];$msg=$_POST['text'];$query=mysql_query("INSERT INTO chat_tbl (`msg`,`name`) VALUES ('$msg','$name')");if(!$query){ echo "Error";}else{ echo "Successfully inserted...";}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2010 Share Posted November 7, 2010 Once per second might be too much for your server, try increasing the interval between loads. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.