Jump to content

$_POSTs remaining empty?!


Topsy Turvey

Recommended Posts

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

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

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.