Creating a simple PHP guestbook. How do you retrieve specific info from text file and echo it to another php page?

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"><p>View Guestbook</p></a> <p>|</p> <a href="form.php"><p>Leave a Message</p></a> <form name="form" 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 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>' . $errors . '<br/></div>'; } ?> </p> 

index.php

<h1>My Guestbook</h1> <a href="index.php"><p>View Guestbook</p></a> <p>|</p> <a href="form.php"><p>Leave a Message</p></a> <p> <?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

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like