misfitxnet Posted May 13, 2009 Share Posted May 13, 2009 I am working on a script to submit and display comments on a web page. The way it is setup is that the user writes comments in two text inputs, and ithe script submits the output to a textfile. I am having trouble though. So far, I can not submit the data so that it creates a new line for each submission. The code I am using is: <?php $name = $_POST['name']; $comment = $_POST['comment']; $output = $name."\t".$comment."\t"."\n"; $fp = fopen("d:\hshome\c239198\local54memberforum.com\info.txt", "ab"); fwrite($fp, $output); ?> This will work, except for the \n. The output created is a single line in info.txt. So i try and work with this to display the content of output.txt First I make it into a string, and then try to manipulate it using: <?php $commenttext=file_get_contents("d:\hshome\c239198\local54memberforum.com\info.txt"); list($name, $comment) = split("\t", $commenttext); echo "<tr bgcolor=6699CC>". "<td>Name: $name</td>\n"."</tr>". "<tr bgcolor=0066CC>". "<td>Comment: $comment</td>\n"."</tr>\n"; ?> But this will only return the first entry, and nothing else. My question is, how do I set this up to display each entry in a seperate table row, every time someone submits something? Is there a better way to format the text file? Is using list() and split() my best options? Thanks Link to comment https://forums.phpfreaks.com/topic/157950-creating-a-comment-script/ Share on other sites More sharing options...
installer69 Posted May 13, 2009 Share Posted May 13, 2009 Is this any help? For the submit comment part: <?php $name = $_POST['name']; $comment = $_POST['comment']; $nametag = 'Name: '; $commenttag = 'Comment: '; $endtag = '<br />'; $output = $nametag.$name.$endtag.$commenttag.$comment.$endtag.$endtag; $fp = fopen("feedback.txt", "a"); fwrite($fp, $output); echo '<b>Please enter feedback: '; echo '<form method="POST" action="feedback.php">'; echo 'Name: <INPUT TYPE="TEXT" NAME="name" size="35" maxlength = "10"><br />'; echo 'Comments: <INPUT TYPE="TEXT" NAME="comment" size="35" maxlength = "10"><br />'; echo '<INPUT TYPE="SUBMIT" VALUE="Submit" NAME="submit">'; echo '</form></b>'; ?> and to read it: <?php $myFile = "feedback.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; ?> I kinda thought to format it before it goes into the textfile so when you call it out it just is as it is. I'm sure there are much better ways though. I haven't much experience with storing stuff in a textfile as I've always (tried to) use mySql. Link to comment https://forums.phpfreaks.com/topic/157950-creating-a-comment-script/#findComment-833174 Share on other sites More sharing options...
installer69 Posted May 13, 2009 Share Posted May 13, 2009 Sorry to re answer an answer but I couldn't see an edit post button (humble apologies Mods) See link here: http://www.landroverkent.com/feedback.php For the code I posted in action. I added a bit of ant caching to the page to make sure it gets the latest comments added and here is the full code. Submit comments page (feedback.php): <?php ?> <html> <head> <title>Leave Comments</title> <meta http-equiv="Expires" content="28FEB2002"> </head> <body> <?php echo '<b>Please enter feedback: '; echo '<form method="POST" action="feedback1.php">'; echo 'Name: <INPUT TYPE="TEXT" NAME="name" size="35" maxlength = "10"><br />'; echo 'Comments: <INPUT TYPE="TEXT" NAME="comment" size="35" maxlength = "10"><br />'; echo '<INPUT TYPE="SUBMIT" VALUE="Submit" NAME="submit">'; echo '</form></b>'; ?> </body> </html> and show comments (feedback1.php): <?php $name = $_POST['name']; $comment = $_POST['comment']; $nametag = 'Name: '; $commenttag = 'Comment: '; $endtag = '<br />'; $output = $nametag.$name.$endtag.$commenttag.$comment.$endtag.$endtag; $fp = fopen("feedback.txt", "a"); fwrite($fp, $output); ?> <html> <head> <title>Comments</title> <meta http-equiv="Expires" content="28FEB2002"> </head> <body> <h1>Comments from our much loved visitors</h1> <?php $myFile = "feedback.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; ?> <a href="feedback.php">Enter more comments</a> </body> </html> Link to comment https://forums.phpfreaks.com/topic/157950-creating-a-comment-script/#findComment-833180 Share on other sites More sharing options...
installer69 Posted May 13, 2009 Share Posted May 13, 2009 OK, all on one page and appending to the top not the bottom: <?php echo '<html><head><title>Comments</title><meta http-equiv="Expires" content="28FEB2002"></head><body>'; echo '<h2>Please leave your comments:</h2>'; echo '<form method="POST" action="feedback2.php">'; echo 'Name: <INPUT TYPE="TEXT" NAME="name" size="35" maxlength = "10"><br />'; echo 'Comments: <INPUT TYPE="TEXT" NAME="comment" size="35" maxlength = "10"><br />'; echo '<INPUT TYPE="SUBMIT" VALUE="Submit" NAME="submit">'; echo '</form></b>'; if ($_POST['name'] != "" && $_POST['comment'] != "") { $name = $_POST['name']; $comment = $_POST['comment']; $nametag = 'Name: '; $commenttag = 'Comment: '; $endtag = '<br />'; $output = $nametag.$name.$endtag.$commenttag.$comment.$endtag.$endtag; if (!file_exists("feedback1.txt")) touch("feedback1.txt"); $fh = fopen("feedback1.txt", "r"); $fcontent = fread($fh, filesize("feedback1.txt")); $towrite = "$output$fcontent"; $fh2 = fopen('feedback1.txt', 'w+'); $fh3 = fopen('feedback2.txt', 'w+'); fwrite($fh2, $towrite); fwrite($fh3, $towrite); fclose($fh); fclose($fh2); fclose($fh3); $name = ''; } echo '<h2>Previous comments left:</h2>'; $fb = fopen("feedback2.txt", 'r'); $theData = fread($fb, filesize("feedback2.txt")); fclose($fb); echo $theData; echo '<a href="feedback22.php">Enter more comments</a></body></html>'; ?> in action here: http://www.landroverkent.com/feedback2.php you will need 2 text files- in my case feedback1.txt and feedback2.txt as I found that the fread would use the file size of feedback1.txt before the new comment was added and would drop the same amount of bytes from the end of the file!! This way it writes to both files but reads the size of feedback2.txt. Seems to work but not sure if thatt's what you were looking for. Prevented caching an no input submissions and set $name to empty to prevent resubmission of duplicate data if page refreshed Link to comment https://forums.phpfreaks.com/topic/157950-creating-a-comment-script/#findComment-833260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.