I am trying to create a simple PHP guestbook where a user enters their information, the information is then validated and written to a text file. I was able to get the information the user enters written to the text file, but then I am having trouble echoing it out to the page I want since all the information spits out as one line.
So the user enters the information in form.php and I want to echo it to index.php. I want to echo out the name and message of each entry, not the email, on separate lines in almost a blog-like format, this way it shows like a guestbook. Right now I have the information in the text file, on different lines but I only want to select a few on the lines, not every single one (in this case the first and third line of each entry).
And also is there a way to include the time of each entry on index.php as well?
*It should be done without a database.
form.php
<h1>My Guestbook</h1>
<a href="index.php" class="gb-link"><p>View Guestbook</p></a>
<p class="divider">|</p>
<a href="form.php" class="msg-link"><p>Leave a Message</p></a>
<form name="form" class="" action="form.php" method="post">
<label for="">
<h5>Name</h5>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" placeholder="Name">
<br>
</label>
<label for="">
<h5>Email</h5>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" placeholder="Email">
<br>
</label>
<label for="">
<h5>Message</h5>
<textarea name="message" rows="8" cols="40" value="<?php echo $_POST['message']; ?>" placeholder="message"></textarea>
<br>
</label>
<br>
<input class="submit" type="submit" name="submit" value="Submit">
</form>
<p>
<?php
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/>';
}
}
else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter your message.<br/>';
}
}
else {
$errors .= 'Please enter your message.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
}
else {
$errors .= 'Please enter your email address.<br/>';
}
if (!$errors) {
$mail_to = 'me@somewhere.com';
$subject = 'Email from Form';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= "message:\n" . $_POST['message'] . "\n\n";
mail($to, $subject, $message);
$guests = fopen('guests.txt', 'a+')
OR die ("Can't open file\n");
fwrite ($guests, $_POST["name"] . "\n");
fwrite ($guests, $_POST["email"] . "\n");
fwrite ($guests, $_POST["message"] . "\n");
fclose($guests);
header('Location: thank-you.php');
exit;
}
else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
?>
</p>
index.php
<h1>My Guestbook</h1>
<a href="index.php" class="gb-link"><p>View Guestbook</p></a>
<p class="divider">|</p>
<a href="form.php" class="msg-link"><p>Leave a Message</p></a>
<p class="rsps">
<?php
$guests = fopen("guests.txt", "r") or die("Unable to open file!");
echo fread($guests,filesize("guests.txt"));
fclose($guests);
?>
</p>