How to prevent fwrite() if no data is entered into form

FYI: I registered, waiting a bit for the confirmation email, requested a new confirmation, still haven’t received anything (nothing in junk mail folder either). Thankfully, you allow posts w/o registration makes it easy for cases like this.


I’m a php rookie, just learning the basics. I’m working on a simple file to help me better understand some of the principles behind the coding.

What I’m trying to do here, is have an html form where the user enters their first and last name, followed by the name of their pet. When they submit, it writes to a .txt file and prints out the names entered. This works just fine.

However, it also works when there is a blank entry (that is, when the variable has no data). I’ve used the if(empty()) function and tried to “break;” it if it is. But it doesn’t seem to work for me.

What I’d like it to do is not write to the .txt file if there is an empty field. The html is a simple form:

[code]

First Name:

Last Name:

Pet's Name:

[/code]

And here is the php code (write_names.php)I have thus far (commented like crazy to help me remember what I’m doing. :wink:

[php]<?php

$filename = 'data/' . 'names.txt';

$fp = fopen($filename, 'a'); // opens file for appending

$cntr = 1;

while(true)
{
	$name_html = 'name' . $cntr;
	$name = $_POST[$name_html];

	if (empty($name))
	{
		break;
	}

	$cntr++;

	// Printing to screen
	// print "Name: " . $name . "<br />";

	$output_line = $name . " ";

	fwrite($fp, $output_line);

	

}
	fwrite($fp, "\n");
	// debug
	// print "name: '" . $name . "'";
	fclose($fp);


	//*****************
	// Reading File
	//*****************

	// looks at FILE $filename, COUNTs the # of line [breaks], and stores that # in var $lines_in_file
	// useful for when wanting to use FOR loop, and process 1 line in file at a time
	$lines_in_file = count(file($filename));

	// FOPEN will file open the file in the $var; "r" is for "reading" the file, then stores into $fp (file pointer)
	$fp = fopen($filename, 'r');   //opens the file for reading

	for ($ii = 1; $ii <= $lines_in_file; $ii++)
	{
		$line = fgets($fp);  //Reads one line from the file
		$names = trim($line); // gets rid of any whitespace to left or right of the line

		print $names.'<br />';
	}

	//closes file completely
	fclose($fp);

?>[/php]

Thanks in advance for any help provided.

You’re doing things in reverse order. Before you do any writing to files, you need to check to see if $_POST is empty, you’re not checking it until after its written. And unless its for an assignment or something, this can be done on 1 page.

That should prevent the file from being changed if any of the names are empty. I took out the redundant stuff
php code [php]
// top of page
if(isset($_POST[‘submit’])) {
if(empty($_POST[‘name1’]) || empty($_POST[‘name2’]) || empty($_POST[‘name3’])) {
echo “One or more of the name fields are empty”;
} else {
$filename = ‘data/’ . ‘names.txt’;
$cntr = 1;
$fp = fopen($filename, ‘a’); // opens file for appending
$name_html = “name”.$cntr."\n";
//echo “name: '” . $name . “’”;
$cntr++;

  $output_line = $name_html;
  fwrite($fp, $output_line);

  fclose($fp);
  //*****************
  // Reading File
  //*****************
  /* looks at FILE $filename, COUNTs the # of line [breaks], and stores that # in var $lines_in_file
  useful for when wanting to use FOR loop, and process 1 line in file at a time*.

  $lines_in_file = count(file($filename));
  // FOPEN will file open the file in the $var; "r" is for "reading" the file, then stores into $fp (file pointer)
  $fp = fopen($filename, 'r');
  //opens the file for reading
  for ($ii = 1; $ii <= $lines_in_file; $ii++) {
     $line = fgets($fp);  //Reads one line from the file
     $names = trim(rtrim($line)); // gets rid of any whitespace to left or right of the line
     echo $names.'<br />';
  }
  //closes file completely
  fclose($fp);

}
}

First Name:

Last Name:

Pet's Name:

[/php]

An easier way of displaying the contents of a file is
[php]
$File = “log.txt”;
$myFile = file_get_contents($File);

$lines = explode("\r\n", $myFile);

foreach ($lines as $line) {
echo nl2br($line);
}[/php]
My file name is different, but that’s the code i use to display a change log that’s stored in a txt file. Doing it 1 line at a time isn’t all that efficient.

I always try to list all my defaults before i do anything outside of validation, it keeps things organized.

I realize there is a way to do it on one page. But the tutorial I’m following starts me with 2 pages (1 for the form, 1 for the coding) to understand the principles behind getting data from forms, so I’m trying my best to stick with that. This is a practice exercise from a Learning PHP dvd from Infinite Skills. I’ve done what it asked for…but I’m trying to add to it…do something more than it asks. Specifically disallow any writing to the .txt file when a blank field is submitted.

So while I will use your 1 page method for future reference and technique, I have applied the isset condition to the .php page and it worked fine. It’s frustrating though because that technique was never explained in the tutorial. I was trying to use the same validation that was taught in other tutorials, I figured I could apply it here. I still don’t fully understand WHY they didn’t work here but did the others. But as I said, I’m way nub, so there is going to be a lot that I’m not going to fully understand now…that I may later down the road.

I appreciate your help though, it did solve the problem and got it to work out great (something I’d been struggling with for around 2.5 hours).

btw…WHY is it validating the way it does? In other words…what is new to me here is the use of the ‘isset’? What is it doing to the name “submit”, which is the submit button?

Also, the || is just saying “or”, correct? Is there a difference between ‘||’ and ‘OR’?

isset = is set, basically, its looking to see if the submit was pressed, that way any code that’s under it won’t be run until there’s user interaction.

There is a difference, but in this case, the result would be the same. If you really want to get into it, go http://www.php.net/manual/en/language.operators.precedence.php. It has more to do with how its being used.

Thank you for the help.

One last question.

If I’m using 2 pages (1 html form + 1 php), then isset isn’t really necessary is it? I can just wrap the entire code with the if(empty) condition and it would work just fine it seems. So then, it would seem that the isset is only in the case of everything being on 1 .php page (where the form is included as well)…is this correct?

Yes, since you’re doing it on two pages, then you can remove if(isset()).

Sponsor our Newsletter | Privacy Policy | Terms of Service