zeth369 Posted May 14, 2009 Share Posted May 14, 2009 I'm very new to php, I just started learning it about a week ago. I'm just starting to get a hang of the basics and i decided to try to create a basic news system without using mysql. basically what im trying to do is be able to use a form, have the info from the form go into a file, and read the file and print it to another page. The problem im having is that every time i refresh the page, it prints it again and again and again. please help!! form page <html> <body> <form method="POST" action="main.php"> Author: <input type="text" name="author" /> <br> News: <input type="text" name="news" /> <input type="submit" value="submit news" /> </form> </body> </html> code to print news: <?php $author = $_POST["author"]; $zeth = file_put_contents("test.php","<center>Author: " . $author . "<br>" . $_POST["news"] . "<br><hr><br></center>", FILE_APPEND); ?> also: <?php $file = filesize("test.php"); $news = file_get_contents("test.php"); $finalnews = wordwrap("$news",75,"<br>"); print $finalnews; ?> Link to comment https://forums.phpfreaks.com/topic/158187-basic-news-system-help/ Share on other sites More sharing options...
Brian W Posted May 14, 2009 Share Posted May 14, 2009 conditional statement needed... <?php if(isset($_POST['author'])){ $author = $_POST["author"]; $zeth = file_put_contents("test.php","<center>Author: " . $author . "<br>" . $_POST["news"] . "<br><hr><br></center>", FILE_APPEND); } ?> Link to comment https://forums.phpfreaks.com/topic/158187-basic-news-system-help/#findComment-834389 Share on other sites More sharing options...
zeth369 Posted May 14, 2009 Author Share Posted May 14, 2009 it still prints the last thing submitted everytime you refresh the page Link to comment https://forums.phpfreaks.com/topic/158187-basic-news-system-help/#findComment-834398 Share on other sites More sharing options...
Brian W Posted May 14, 2009 Share Posted May 14, 2009 delete the file you are writing to (clean start), and post to it again. How many news items do you have? should be one... Post a new one. How many do you have? should be two... do that a few times and see what you get and let us know. btw, I suggest this code for saving: <?php if(isset($_POST['author'])){ $author = $_POST["author"]; $zeth = file_put_contents("test.php","<center>Author: " . $author . "<br>" . wordwrap($_POST["news"], 75, "<br />") . "<br><hr><br></center>", FILE_APPEND); } ?> and this code to output the file's content: <?php $file = filesize("test.php"); $news = file_get_contents("test.php"); print $news; ?> Link to comment https://forums.phpfreaks.com/topic/158187-basic-news-system-help/#findComment-834435 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.