代码之家  ›  专栏  ›  技术社区  ›  sanoj lawrence

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

  •  -2
  • sanoj lawrence  · 技术社区  · 6 年前

    AM使用以下代码创建聊天系统并将数据存储到 txt 文件。它可以满足我的需要,但在提交数据时,当我键入文本并按回车键时,会出现一个小错误 AJAX 从文件加载数据 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>
    

    聊天.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);
    }
    

    有人能帮我分类错误吗,为什么 阿贾克斯 从加载数据 聊天数据.txt 同时提交数据。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Krishanu    6 年前

    如果你用 index.html 而不是 index.php ,这不起作用!

    之后,你可能会忘记改变 "chatdata.txt" 22号线到 $nametxt 是的。

    我试过改变,结果成功了!啊!