Form script PFWRITE loops twice - HELP!

A

Anonymous

Guest
Hi Folks,

I have a little script that reads three HTML form fields and writes the contents to a text file. But the darn thing always writes an empty string (except for 3 commas) before it writes the POSTed contents. So it's looping through twice for each execution of the PHP file. It's driving me nuts. Can someone set me straight?

Here's a copy of the script:
Code:
<?php
echo '<html><head><title>Test Write</title></head> 
<body> 
<form action="Form_Write_Text_File.php" method="POST">
<p><input type="text" name="v0" size="20"></p>
<p><input type="text" name="v1" size="20"></p>
<p><input type="text" name="v2" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form> 
</body> 
</html>';
$hFile = fopen( "form_results.txt", "a" ); 
if (!$hFile = @fopen ("form_results.txt","a")) die ("No file opened");
$v0 = $_POST["v0"];
$v1 = $_POST["v1"];
$v2 = $_POST["v2"];
$values = "$v0,$v1,$v2,";
$numBytes = fwrite($hFile,$values) or die("Couldn't write values to file!");
fclose($hFile); 
?>

You can copy this script and try it yourself; be sure to create an empty results file named form_results.txt in the same directory.

Yes, I'm a newbie.
 
Try this out:
Code:
<?php

if(isset($_POST['B1'])){
     if($hFile = fopen("form_results.txt", "a")){ 
          $values = $_POST["v0"] . ',' . $_POST["v1"] . ',' . $_POST["v2"] . ','; 
          $numBytes = fwrite($hFile, $values) or die("Couldn't write values to file!"); 
          fclose($hFile); 
     }
     else{
          print "No file opened";
     }
}

?>
Note: Please use code tags next time.
 
Back
Top