As you can probably guess, I am new to learning PHP and have an assignment to create a page where new user accounts can be created by entering the username and password into form fields, which then needs to be saved in a text document.
I have spent hours trying to figure this out and have tried what I have seen suggested on other questions. As I mentioned previously, I am very new to this and need to keep things basic.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$newUsername = trim(htmlspecialchars($_POST["newuser"]));
$newPassword = trim(htmlspecialchars($_POST["newpwd"]));
$file = "user_accounts.txt";
$openfile = fopen($file, "a") or die("Unable to open file");
if($newUsername || $newPassword == ""){
echo '<script language="javascript">';
echo 'alert("Error: Please complete all fields")';
echo '</script>';}
else{
fwrite ($openfile, "$newUsername
$newPassword
");
fclose($openfile);}
}
?>
And here is my form code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
Username: <input type="text" name="user"><br>
Password: <input type="password" name="pwd"><br>
<input type="submit">
</form>