php - 尝试将文本附加到文本文件中的新行时出错
问题描述
我正在使用 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>
解决方案
"您使用 "userMessage\r\n" 作为字段名称。如果要在字段后添加新行,请使用 $_POST["userMessage"] 。"\r\n" – Nigel Ren 1 小时前"
推荐阅读
- html - 在哪里将退订电子邮件令牌放在 html 代码中?
- python - 如果请求的索引在整数索引标签之间,pandas 数据帧中是否有内置方式来包含切片的下一行/上一行?
- ios - 将浮点数列表从文件加载到 MLMultiArray
- django - django switch 语言不起作用(i18n)
- c++ - 为什么我应该在下面的例子中省略大括号?
- django - Django Api Json 格式更改
- python - 如何在opencv python中获得低对比度图像的边缘
- postgresql - PostgreSQL:选择没有页眉/页脚行的语句
- python - 高波动数据优化与预测
- flutter - 无法使用 GetX 显示下拉菜单按钮值