首页 > 解决方案 > 尝试将文本附加到文本文件中的新行时出错

问题描述

我正在使用 webhosting000 作为第六种计算单元。这是我第一次使用 PHP,所以请原谅任何初学者错误,当我尝试将 $userInput 附加到 UserNotes.txt 时,我收到一条错误消息。

注意:未定义索引:第 34 行 /storage/ssd3/381/15908381/public_html/index.php 中的 userMessage

我已经阅读了 PHP 网站和 W3schools 关于使用 \n 和 \r\n 追加到新行的内容,但我无法在我的代码中找到问题的根源。我需要做些什么来解决此错误并能够将用户的消息保存在文本文件中的新行上?

<!DOCTYPE html>
<html>
    <body>
        <h1>This Is a Website</h1>
            <?php 
                echo "I wrote this using php";
            ?>
        <br>
        <h2>Your IP Address:</h2>
            <?php
                $user_IP = $_SERVER['REMOTE_ADDR'];
                echo $user_IP;
            ?>
        <br>
        <h2>Your browser Information:</h2>
            <?php
                $browserInfo = $_SERVER['HTTP_USER_AGENT'];
                echo $browserInfo;
            ?>
        <br>
        <h2>Your Screen Information</h2>
            <script>
                var screenWidth = window.screen.width;
                var screenHeight = window.screen.height;
                document.write("Screen width: " + screenWidth + "px" + "<br />");
                document.write("Screen height: " + screenHeight + "px" + "<br />");
            </script>
        <br>
        <h2>Send me a message!</h2>
            <?php
                $file="userNotes.txt";
                if(isset($_POST["submitButton"]))
                {
                    $userInput = $_POST["userMessage\r\n"];
                    file_put_contents($file, $userInput, FILE_APPEND | LOCK_EX);
                }
            ?>
            <form action="" method="post">
                <input type="text" name="userMessage">
                <input type="submit" value="send" name="submitButton">
            </form>
    </body>
</html>

标签: phpwebserver

解决方案


"您使用 "userMessage\r\n" 作为字段名称。如果要在字段后添加新行,请使用 $_POST["userMessage"] 。"\r\n" – Nigel Ren 1 小时前"


推荐阅读