Jump to content

problem when combinating JS, PHP and HTML


Worqy

Recommended Posts

Hi.

I working on a chat program.

Now I get a problem when I use JS for updating the chat message textarea.

Here is the codes:

<?php
/*
chat.php
*/
Session_start();
?>
<head>
<!-- Javascript -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
function updateTable() {
	$("#chatlog").load('getmessage.php');
	}
	setInterval("updateTable()",1000);
</script>	
</head>
<form method="POST" action="sendmessage.php">
<body bgcolor="gray">
<center><h2>Chat</h2></center>
<center>
<textarea name="chatlog" id="chatlog" cols=70 rows=20>

</textarea>
<textarea name="chatmessage" cols=62 rows=1>
</textarea>
<input type="submit" name="send" value="Send">
<center>
<a href="getmessage.php">Get Message</a>
</body>
</form>

 

<?php
/*
getmessage.php
*/
Session_start();

include 'config.php';

// Connect to MySQL
$connect = mysql_connect("$host", "$username", "$password")or die("Cannot connect to MySQL");
mysql_select_db("$database")or die("Cannot select database!");

$sql = mysql_query("SELECT * FROM messages")or die(mysql_error());
while($row = mysql_fetch_array( $sql ))
{
$_SESSION['user'] = $row['user'];
$_SESSION['message'] = $row['message'];
echo "[" . $row['time'] . "]" . $row['user'] . " > " . $row['message'];
}	
?>

 

Now I have two problems.

1) When I post a new message every message comes on the same row in the textarea. And if I edit the code like this:

echo "[" . $row['time'] . "]" . $row['user'] . " > " . $row['message'] . "<br />";

It post the <br /> in the textarea.

2) All the messages are posted in the wrong ord.

It starts from the oldest  message and show the newest last ( This might be fixed if my problem one ( 1 ) is fixed )

 

// Kevin

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.