我目前正在PHP项目中构建一个非常简单的AJAX聊天室,设计师可以与客户进行交互。
问题在于,保存消息功能确实有效,但当我尝试显示所有已保存的消息时,它只显示在聊天部分:Page not found
。我的猜测是问题在XMLHttpRequest
范围内,但我似乎找不到更多我想要的解决方案。
我的主要PHP文件(聊天所在的位置):
<aside class="contentcomment">
<h1>Comments</h1>
<div class="input-form">
<!--div to recieve the certain errors-->
<div id="error">
</div>
<!--div to send the message-->
<div id="write">
<input type="text" id="text" maxlength="255" placeholder="Comment..."/>
<input type="hidden" id="name" value="<?php echo ($myusername);?>">
<input type="hidden" id="client" value="<?php echo ($client);?>">
<input type="hidden" id="designid" value="<?php echo ($designid);?>">
<input type="button" id="commentbutton" value="Send" onclick="send();"/>
</div>
<!--div to recieve the messages-->
<ul class="chats">
<div id="messages"></div>
</ul>
</div> <!--end demo-wrapper-->
<script type="text/javascript">
//This function will display the messages
function showmessages(){
//Send an XMLHttpRequest to the 'show-message.php' file
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","show-messages.php",false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","show-messages.php",false);
xmlhttp.send();
}
//Replace the content of the messages with the response from the 'show-messages.php' file
document.getElementById('messages').innerHTML = xmlhttp.responseText;
//Repeat the function each 30 seconds
setTimeout('showmessages()',30000);
}
//Start the showmessages() function
showmessages();
//This function will submit the message
function send(){
//Send an XMLHttpRequest to the 'send.php' file with all the required informations
var sendto = 'send.php?message=' + document.getElementById('text').value +
'&name=' + document.getElementById('name').value + '&client=' + document.getElementById('client').value + '&designid=' + document.getElementById('designid').value;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",sendto,false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",sendto,false);
xmlhttp.send();
}
var error = '';
//If an error occurs the 'send.php' file send`s the number of the error and based on that number a message is displayed
switch(parseInt(xmlhttp.responseText)){
case 1:
error = 'The database is down!';
break;
case 2:
error = 'The database is down!';
break;
case 3:
error = 'Don`t forget the message!';
break;
case 4:
error = 'The message is too long!';
break;
case 5:
error = 'Don`t forget the name!';
break;
case 6:
error = 'The name is too long!';
break;
case 7:
error = 'This name is already used by somebody else!';
break;
case 8:
error = 'The database is down!';
}
if(error == ''){
document.getElementById('error').innerHTML = '';
showmessages();
}
else{
document.getElementById('error').innerHTML = error;
}
}
</script>
</aside>
然后这是我的“show-message.php”:
<?php
// All the connection info goes here...
// Connect to server and select databse.
$connect = mysql_connect("$host", "$username", "$password")or die(1);
mysql_select_db("$db_name")or die(2);
//Get the first 10 messages ordered by time
$result = mysql_query("SELECT * FROM Comment_Uploads ORDER BY Time DESC LIMIT 0,10");
$messages = array();
//Loop and get in an array all the rows until there are not more to get
while($row = mysql_fetch_array($result)){
//Put the messages in divs and then in an array
$messages[] = "<li id=\"client\">" . $row[Comment] . "</li>";
//The last posts date
$old = $row[time];
}
//Display the messages in an ascending order, so the newest message will be at the bottom
for($i=9;$i>=0;$i--){
echo $messages[$i];
}
//This is the more important line, this deletes each message older then the 10th message ordered by time, so the table will never have to store more than 10 messages.
//mysql_query("delete from chat where time < " . $old);
?>