我正在我的网站上进行聊天,我的代码允许用户输入文本并按Enter键提交。输入与使用php将输入写入文件一起工作,但是应该将文件中的输入添加到其中的函数似乎不起作用。它会添加用户的昵称,但不会显示用户输入的消息。我究竟做错了什么?
我已经尝试过2天了。先感谢您!
的Javascript / JQuery的:
var instanse = false;
var file = 'data.txt'
function Chat() {
/* call update chat function */
this.update = updateChat;
/* call send chat function */
this.send = sendChat;
/* call get state function */
this.getState = getState;
}
//define get state function
function getState() {
if(!instanse){
instanse = true;
/* define AJAX function */
$.ajax({
type: "POST",
url: "ajax.php",
data: {'function': 'getState', 'file': file},
dataType: "json",
success: function(data) {state = data.state;instanse = false;}
});
}
}
//define update chat function
function updateChat() {
if(!instanse){
instanse = true;
/* define AJAX function */
$.ajax({
type: "POST",
url: "ajax.php",
data: {'function': 'update','state': state,'file': file},
dataType: "json",
success: function(data) {
if(data.text){
/* manage data */
for (var i = 0; i < data.text.length; i++) {
$('#chat-row').append($(""+ data.text[i] +""));
}
}
/* manage position of current chat */
document.getElementById('chat-row').scrollTop = document.getElementById('chat-row').scrollHeight;
instanse = false;
state = data.state;
}
});
}
else {
setTimeout(updateChat, 1000);
}
}
//define send chat function
function sendChat(msg, name) {
updateChat();
/* define AJAX function */
$.ajax({
type: "POST",
url: "ajax.php",
/* manage message */
data: {'function': 'send','message': msg,'nickname': name,'file': file},
dataType: "json",
success: function(data){
updateChat();
}
});
}
//--------------------
// open popup prompt for ask name of user
var name = prompt("Enter your name:", "Guest");
// default name is 'Guest'
if (!name || name === ' ') {
name = "Guest";
}
// strip tags
name = name.replace(/(<([^>]+)>)/ig,"");
// display name on page
$("#user-name").html("User: <strong>" + name + "</strong>");
var chat = new Chat()
$(function() {
chat.getState();
/* define function when key presses */
$("#posttext").keydown(function(event) {
var key = event.which;
/* if key including return. */
if (key >= 33) {
var maxLength = $(this).attr("maxlength");
var length = this.value.length;
/* define limit of new content */
if (length >= maxLength) {
event.preventDefault();
}
}
});
/* define function when key release */
$('#posttext').keyup(function(e) {
if (e.keyCode == 13) {
var text = $('#posttext').val();
var maxLength = $(this).attr("maxlength");
var length = text.length;
// send
if (length <= maxLength + 1) {
chat.send(text, name);
$(this).val("");
}
else {
$(this).val(text.substring(0, maxLength));
}
}
});
});
PHP:
<?php
$function = $_POST['function'];
$log = array();
switch($function) {
/* get state case */
case('getState'):
/* check file available or not */
if (file_exists('data.txt')) {
/* assign to variable */
$lines = file('data.txt');
}
$log['state'] = count($lines);
break;
/* update case */
case('update'):
$state = $_POST['state'];
/* check file available or not */
if (file_exists('data.txt')) {
/* assign to variable */
$lines = file('data.txt');
}
$count = count($lines);
if ($state == $count){
/* if state & count are equal */
$log['state'] = $state;
$log['text'] = false;
} else {
/* if state & count are no equal */
$text= array();
$log['state'] = $state + count($lines) - $state;
foreach ($lines as $line_num => $line) {
if ($line_num >= $state){
$text[] = $line = str_replace("\n", "", $line);
}
}
$log['text'] = $text;
}
break;
/* send case */
case('send'):
$nickname = htmlentities(strip_tags($_POST['nickname']));
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$message = htmlentities(strip_tags($_POST['message']));
if (($message) != "\n") {
if (preg_match($reg_exUrl, $message, $url)) {
$message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
}
fwrite(fopen('data.txt', 'a'), "<div>". $nickname . "</div> \n" . $message);
}
break;
}
echo json_encode($log);