I'm battling with having to add data into a txt file with php. It gives the the following error:
//saving info in txt file. Warning : Undefined array key "User" in C:\xampp\htdocs\web_project2022\login.php on line 89 Fatal error : Uncaught Error: Undefined constant "n" in C:\xampp\htdocs\web_project2022\login.php:97 Stack trace: #0 {main} thrown in C:\xampp\htdocs\web_project2022\login.php on line 97**
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="stylesheet/style.css">
<title>Login Page </title>
</head>
<header>
<img src="images/E-SHELF.png" alt="logo" class="logo">
<div class="container">
<nav>
<li><a href="index.php"> HOME</a><li>
<li><a href="singup.php"> Signup</a><li>
<li><a href="admin.php"> Login as Adim</a><li>
</nav>
</div>
</header>
<body>
<div class="login_container">
<?php
include "function.php";
$_SESSION["cart"] = [];
$userName = "";
$userPassword = "";
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userName = $_POST['Username'];
$userPassword = $_POST['Password'];
//"Email or Password Incorrect!"
function validate($userName, $password)
{
$name = trim($userName);
$pass = trim($password);
$name = stripslashes($name);
$pass = stripslashes($pass);
$name = htmlspecialchars($name);
$pass = htmlspecialchars($pass);
$hash_pass = md5($pass);
if ($name == "root@gmail.com" && $hash_pass ==
"827ccb0eea8a706c4c34a16891f84e7b") {
header("Location:index.php");
} else {
return "Email or Password Incorrect!";
}
};
// $error = validate($_POST['Username'], $_POST['Password']);
};
echo <<<FORM
<form action="index.php" method="post" class="login-form">
<h2> Login</h2>
<p>$error</p>
<label>Username</label>
<input type="email" name="Username" placeholder="Username" required
value="$userName">
<br>
<label>Password</label>
<input type="password" name="Password" minlength="4"
placeholder="Password" value="$userPassword">
<br>
<button type="Submit">Login</button>
</form>
?>
<?php
$userName=$_POST["User"];
error_reporting(E_ERROR|E_PARSE);
$Password=$_POST["Password"];
$file=fopen("UserData.txt" , "a");
fwrite($file,"name");
fwrite($file ,$Username. \n );
fwrite($file,"password:");
fwrite($file ,$Pasword. \n );
fclose($file);
?>
</div>
</body>
</html>