Topsy Turvey Posted April 14, 2007 Share Posted April 14, 2007 Hi there! I've got a problem with the below script. In writing to the flat file, only the ||||||s and the date function are being passed through. For some reason, the $_POSTs remain empty, or at least don't 'write'... I have tried different ways of building the $line variable but...nothing Any help will be greatly appreciated. Sorry if it's really obvious. The code: <?php if (isset($_POST['submit'])) { if (empty($_POST['title'])) { echo '<p class="error">Please enter a title<br />'; } elseif (empty($_POST['news'])) { echo '<p class="error">Please enter some news<br />'; } } else { // Setup and save the news item //$line = $_POST['title']; //$line .= "||".$_POST['user']; //$line .= "||".$_POST['news']; //$line .= "||".date('D m.d.y h:i:s A')."\r\n"; $title = $_POST['title']; $user = $_POST['user']; $news = $_POST['news']; $line = $title; $line .= "||".$user; $line .= "||".$news; $line .= "||".date('D m.d.y h:i:s A')."\r\n"; $fp = fopen("news.txt","a"); //if(!$fp) { // echo 'Error: Cannot open file.'; // exit; //} fwrite($fp, $line); fclose($fp); } ?> <form method="post" action="managehome.php"> <p> Title<br/> <input type="text" name="title" size="19" maxlength="35"><br/> User (who you are)<br/> <select name="user"> <option value="1">David</option> <option value="2">Cochise</option> <option value="3">Ben</option> </select><br/> News text<br/> <textarea rows="9" name="news" cols="30"></textarea><br/><br/> <input type="submit" value="Post" name="submit"></p> </form> Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/ Share on other sites More sharing options...
Glyde Posted April 14, 2007 Share Posted April 14, 2007 print_r($_POST). Make sure the data is correctly submitted. Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/#findComment-229201 Share on other sites More sharing options...
kenrbnsn Posted April 14, 2007 Share Posted April 14, 2007 Your "if" statement is incorrect. The writing to the file is only happening if the form hasn't been submitted. Change the "if" to this (I also cleaned up your code a little) <?php if (isset($_POST['submit'])) { if (empty($_POST['title'])) { echo '<p class="error">Please enter a title<br />'; } elseif (empty($_POST['news'])) { echo '<p class="error">Please enter some news<br />'; } else { $tmp = array(); foreach (array('title','user','news') as $item) $tmp[] = trim(stripslashes($_POST[$item])); $tmp[] = date('D m.d.y h:i:s A'); $fp = fopen("news.txt","a"); fwrite($fp, implode('||',$tmp)."\r\n"); fclose($fp); } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/#findComment-229229 Share on other sites More sharing options...
Topsy Turvey Posted April 14, 2007 Author Share Posted April 14, 2007 Ken: Thank you!: Loved what you did with the $item variable. I'm a real noob at this and my code is a bit mental at the minute. Thanks for tidying it up! If you get the chance, I've tried looking all over the place to find out how to convert the select option from a number (David written as '1' in the file). Thanks again - and very fast too. Cheers - H Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/#findComment-229241 Share on other sites More sharing options...
kenrbnsn Posted April 14, 2007 Share Posted April 14, 2007 Just put what ever value you want in the "option" tag. It doesn't need to be a number: <select name="user"> <option value="David">David</option> <option value="Cochise">Cochise</option> <option value="Ben">Ben</option> </select><br/> Ken Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/#findComment-229243 Share on other sites More sharing options...
Topsy Turvey Posted April 14, 2007 Author Share Posted April 14, 2007 Whoops!! I was hastily trying to apologise for the stupidity of that last question. Better revise my HTML before I do any more PHP!! Thanks for replying anyway! Link to comment https://forums.phpfreaks.com/topic/46999-_posts-remaining-empty/#findComment-229247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.