Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php AJAX在提交数据时加载奇怪的错误_Php_Ajax - Fatal编程技术网

Php AJAX在提交数据时加载奇怪的错误

Php AJAX在提交数据时加载奇怪的错误,php,ajax,Php,Ajax,我使用以下代码创建聊天系统并将数据存储到txt文件中。它可以满足我的需要,但在提交数据时出现了一个小错误,当我键入文本并点击enter键时,我从文件chatdata.txt加载了一秒钟的数据 Index.html <?php session_start(); //Create a session of username and logging in the user to the chat room if (filter_input(INPUT_POST, 'username')) {

我使用以下代码创建聊天系统并将数据存储到
txt
文件中。它可以满足我的需要,但在提交数据时出现了一个小错误,当我键入文本并点击enter键时,我从文件
chatdata.txt
加载了一秒钟的数据

Index.html

<?php
session_start();
//Create a session of username and logging in the user to the chat room
if (filter_input(INPUT_POST, 'username')) {
    $_SESSION['username'] = filter_input(INPUT_POST, 'username');
}
//Unset session and logging out user from the chat room
if (isset($_GET['logout'])) {
    unset($_SESSION['username']);
    header('Location:index.php');
}
?>
<html>
    <head>
        <title>Simple Chat Room</title>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,400,300' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="css/style.css" />
        <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    </head>
    <body>
        <div class='header'>
            <h1>
                SIMPLE CHAT ROOM
                <?php // Adding the logout link only for logged in users   ?>
                <?php if (isset($_SESSION['username'])) { ?>
                    <a class='logout' href="?logout">Logout</a>
                <?php } ?>
            </h1>

        </div>

        <div class='main'>
            <?php //Check if the user is logged in or not  ?>
            <?php if (isset($_SESSION['username'])) { ?>
                <div id='result'></div>
                <div class='chatcontrols'>
                    <form method="post" onsubmit="return submitchat();">
                        <input type='text' name='chat' id='chatbox' autocomplete="off" placeholder="ENTER CHAT HERE" />
                        <input type='submit' name='send' id='send' class='btn btn-send' value='Send' />
                        <input type='button' name='clear' class='btn btn-clear' id='clear' value='X' title="Clear Chat" />
                    </form>
                    <script>
                    // Javascript function to submit new chat entered by user
                        function submitchat() {
                            if ($('#chat').val() == '' || $('#chatbox').val() == ' ')
                                return false;
                            $.ajax({
                                url: 'chat.php',
                                data: {chat: $('#chatbox').val(), ajaxsend: true},
                                method: 'post',
                                success: function (data) {
                                    $('#result').html(data); // Get the chat records and add it to result div
                                    $('#chatbox').val(''); //Clear chat box after successful submition
                                    document.getElementById('result').scrollTop = document.getElementById('result').scrollHeight; // Bring the scrollbar to bottom of the chat resultbox in case of long chatbox
                                }
                            })
                            return false;
                        };
                    // Function to continously check the some has submitted any new chat
                        setInterval(function () {
                            $.ajax({
                                url: 'chat.php',
                                data: {ajaxget: true},
                                method: 'post',
                                success: function (data) {
                                    $('#result').html(data);
                                }
                            })
                        }, 1000);
                    // Function to chat history
                        $(document).ready(function () {
                            $('#clear').click(function () {
                                if (!confirm('Are you sure you want to clear chat?'))
                                    return false;
                                $.ajax({
                                    url: 'chat.php',
                                    data: {username: "<?php echo $_SESSION['username'] ?>", ajaxclear: true},
                                    method: 'post',
                                    success: function (data) {
                                        $('#result').html(data);
                                    }
                                })
                            })
                        })
                    </script>
                <?php } else { ?>
                    <div class='userscreen'>
                        <form method="post">
                            <input type='text' class='input-user' placeholder="ENTER YOUR NAME HERE" name='username' />
                            <input type='submit' class='btn btn-user' value='START CHAT' />
                        </form>
                    </div>
                <?php } ?>
                </div>
            </div>
    </body>
</html>

简单聊天室
简单聊天室
//Javascript函数,用于提交用户输入的新聊天记录
函数submitchat(){
if($('#chat').val()=''|$('#chatbox').val()='')
返回false;
$.ajax({
url:'chat.php',
数据:{chat:$('#chatbox').val(),ajaxsend:true},
方法:“post”,
成功:功能(数据){
$('#result').html(数据);//获取聊天记录并将其添加到result div
$(“#聊天室”).val(“”)//成功提交后清除聊天室
document.getElementById('result').scrollTop=document.getElementById('result').scrollHeight;//如果聊天框很长,请将滚动条放到聊天结果框的底部
}
})
返回false;
};
//函数持续检查some是否已提交任何新聊天记录
setInterval(函数(){
$.ajax({
url:'chat.php',
数据:{ajaxget:true},
方法:“post”,
成功:功能(数据){
$('#result').html(数据);
}
})
}, 1000);
//聊天记录功能
$(文档).ready(函数(){
$('#清除')。单击(函数(){
如果(!confirm('您确定要清除聊天吗?'))
返回false;
$.ajax({
url:'chat.php',
数据:{username:,ajaxclear:true},
方法:“post”,
成功:功能(数据){
$('#result').html(数据);
}
})
})
})
Chat.php

<?php
session_start();
$filename=$_SESSION['username'];
$nametxt= "$filename.txt";
$message='created successfully';

if (file_exists($nametxt)) {
  $fh = fopen($nametxt, 'a');
} else {
  $fh = fopen($nametxt, 'w');
  fwrite($fh, $message."\n");
}
fclose($fh);

if(filter_input(INPUT_POST, 'ajaxsend') && filter_input(INPUT_POST, 'ajaxsend')==true){
    // Code to save and send chat
    $chat = fopen($nametxt, "a");
    $data="<b>".$_SESSION['username'].':</b> '.filter_input(INPUT_POST, 'chat')."<br>";
    fwrite($chat,$data);
    fclose($chat);

    $chat = fopen("chatdata.txt", "r");
    echo fread($chat,filesize("$nametxt"));
    fclose($chat);
} else if(filter_input(INPUT_POST, 'ajaxget') && filter_input(INPUT_POST, 'ajaxget')==true){
    // Code to send chat history to the user
    $chat = fopen($nametxt, "r");
    echo fread($chat,filesize("$nametxt"));
    fclose($chat);
} else if(filter_input(INPUT_POST, 'ajaxclear') && filter_input(INPUT_POST, 'ajaxclear')==true){
    // Code to clear chat history
    $chat = fopen($nametxt, "w");
    $data="<b>".$_SESSION['username'].'</b> cleared chat<br>';
    fwrite($chat,$data);
    fclose($chat);
}

如果使用
index.html
而不是
index.php
,这将不起作用

之后,您可能忘记将第22行的
“chatdata.txt”
更改为
$nametxt

我尝试了改变,它成功了

可能重复的