php - Error when trying to append text to a new line within a text file
I am using webhosting000 for a sixth-form computing unit. This is my first time using PHP so please forgive any beginner errors, when I try to append $userInput into UserNotes.txt I am receiving an error message.
Notice: Undefined index: userMessage in /storage/ssd3/381/15908381/public_html/index.php on line 34
I have read through the PHP website and W3schools on the use of \n and \r\n to append to a new line but I cant find the source of the problem within my code. What do I need to do to solve this error and be able to save the user's message within the text file on a new line?
<!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>
Answer
Solution:
"You are using "userMessage\r\n" as the field name. If you want to add a new line after the field, use $_POST["userMessage"] ."\r\n" ??� Nigel Ren 1 hour ago"
Source