Registered users:
Tyler Surline 18 Male
CODE FOLLOWS
<?php
error_reporting(E_ALL);
include('../includes/header.php');
// File takes in a users info, and appends the info into a file
$file = 'text-files/my-example.txt';
if (file_exists($file)) {
$handle = fopen($file, 'r');
if ($handle) {
echo 'File found!';
echo '<br><br><br><br>';
$content = fread($handle, filesize($file));
fclose($handle);
$content = nl2br($content);
echo $content;
echo '<br><br><hr>';
} else {
echo 'Unable to open file!';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Example</title>
</head>
<body>
<form method="post" action="">
<label for="name">Enter First and Last Name: </label>
<input type="text" id="name" name="name"><br><br>
<label for="age">Age: </label>
<input type="number" id="age" name="age"><br><br>
<label for="gender">Gender: </label>
<select name="gender" id="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$age = isset($_POST['age']) ? $_POST['age'] : '';
$gender = isset($_POST['gender']) ? $_POST['gender'] : '';
if (!empty($name) && !empty($age) && !empty($gender)) {
$handle = fopen($file, 'a');
if ($handle) {
fwrite($handle, "\n \n $name $age $gender \n");
fclose($handle);
echo 'File was updated!';
} else {
echo 'Unable to open file!';
}
} else {
echo "Please fill all fields!";
}
include('../includes/footer.php');
?>