简单的ajax聊天
我想做一个非常简单的聊天页面,
无需登录 不使用 sql
只需要 php、html 和 ajax。
首先,我制作了
chat.html
:
<html>
<head>
<script type="text/javascript">
function chat()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chat").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","chatreturn.php?name=getElementById("name").value&data=getElementById("data").value",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Name: <input type="text" id="name" />
writing: <input type="text" id="data" />
<button onclick=chat() >
</form>
<div id=chat></div>
</body>
</html>
和 chatreturn.php
:
<html>
<head>
</head>
<body>
<?php
$logger = fopen("chatlog.txt","w");
$msg = $_GET["name"];
$msg += " " + $_GET["data"];
fwrite($logger,$msg);
fclose($logger);
$logdata = fopen("chatlog.txt","r");
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
document.write($display);
fclose($logdata );
?>
</body>
</html>
我希望每次发送消息时,该消息都会添加到当前文档中,但奇怪的症状是它什么也不返回。
I want to make an very simple chatting page
no login required
no sql going to be used
Only php, html and ajax is needed.
First, I made a
chat.html
:
<html>
<head>
<script type="text/javascript">
function chat()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chat").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","chatreturn.php?name=getElementById("name").value&data=getElementById("data").value",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Name: <input type="text" id="name" />
writing: <input type="text" id="data" />
<button onclick=chat() >
</form>
<div id=chat></div>
</body>
</html>
and chatreturn.php
:
<html>
<head>
</head>
<body>
<?php
$logger = fopen("chatlog.txt","w");
$msg = $_GET["name"];
$msg += " " + $_GET["data"];
fwrite($logger,$msg);
fclose($logger);
$logdata = fopen("chatlog.txt","r");
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
$display += fgets($logdata);
document.write($display);
fclose($logdata );
?>
</body>
</html>
I expect everytime I send a message, the message is added to current document but the weird symtom is that it returns nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
学习如何编写聊天程序固然很好,但我的建议是,您首先需要使用适合该工作的正确工具。
虽然学习如何烤苹果派很不错,但您不需要同时种植一棵苹果树。
使用库可以帮助您使事情变得更加简洁,并专注于项目中真正重要的内容,而不是其周围的具体细节。例如,jQuery 对于您的 XHR 将是一个很好的开始。
处理 XHR 时必须使用 JavaScript 调试器。我建议使用 Chrome 和开发者工具。我个人认为在功能和实用性方面没有任何其他工具可以击败它。
使用数据库来存储您的数据。文件不是一个好主意。
PHP 手册中有很好的示例;向他们学习。
你似乎没有完全掌握 PHP 语法,并且混合了服务器端和客户端代码(另外,你在 GET url 中编写 Javascript 代码?你应该有 Javascript 错误,因为你的引号甚至没有转义)。由于聊天程序需要对这两者有很好的理解,所以先从一些基本测试开始。
Learning how to write a chat program is nice, but my advice is that you need first to use the right tools for the job.
While it is nice to learn how to bake an apple pie, you don't need to also grow an apple tree along with it.
Use libraries to help you make things more concise, and focus on what's really important about your project, and not the nuts and bolt around it. For example, jQuery for your XHR would be a good start.
Javascript debuggers are a must when dealing with XHRs. I suggest Chrome with the developer tools. I personally don't think any other tool can beat it in terms of features and usefulness.
Use a database to store your data. Files are not a good idea.
There are good examples from the PHP manual; learn from them.
You seem to not entirely grasp the PHP syntax, and mix server side and client side code (also, you are writing Javascript code inside your GET url? you should have Javascript errors since your quotes are not even escaped). Since a chat program requires a good understanding of them two, start with some basic tests first.