RaveDash

Creating a simple PHP guestbook. How do you retrieve specific info from text file and echo it to ano

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 = '[email protected]'; $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> 
2 Related questions 37 PHP: How do I display the contents of a textfile on my page? 0 get contents from another page using php? 0 Retrieving information from another page Related questions 37 PHP: How do I display the contents of a textfile on my page? 0 get contents from another page using php? 0 Retrieving information from another page 1 Take data from one php page and echo it onto another page 0 how to get text from other page in php 3 php retrieve data from text file 0 Pulling data out from a text file in PHP 0 list data from text file with php 3 Beginner using php to create guest book that logs info to text file and displays data on separate page 0 Retrieve data inside txt file to use it php script Load 7 more related questions Show fewer related questions

Sorted by: Reset to default

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoa29raWmEcYKOnKmemaSeu6h5wGaqoqWgobJuvMepZKCtlajBo7vOpGShp6disbB52KisZqqVqb%2BqsdWeZKyolZi2p7XCZqCnnp9is7O7zGarnrCkYrOquA%3D%3D

Abbie Anker

Update: 2024-05-21