Hello
I am trying to learn php, I have been looking up information on google, I understand a few things but i need help, I want to get input form an html form and save it to a text file here a snippet of some code I have tried:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/>
    <title>Test</title>
  </head>

  <body>
    <form enctype="text/plain" method="POST" name="TestForm"/>

      <p />
      Input anything: <input type="text" name="anything" value="Default"/> <br />
      <input type="submit" value="OK" name="submit"/>
    </form>
    <?php
     if ($submit=='OK')  {
       $fh = fopen("form_output.txt", 'w');
       fwrite($fh, $_POST['anything']);
       fclose($fh);
     }
   ?>
  </body>
</html>

it shows the form but the output text file is not created I am running php5 on linux.
TIA

Recommended Answers

All 7 Replies

try:

<?php
if( isset($_POST['submit']) && $_POST['submit']=='OK' )
{
	$data=$_POST['anything'].PHP_EOL;
	$file=dirname(__FILE__) ."/form_output.txt";
	if( file_put_contents( $file, $data, FILE_APPEND) )
	{
		echo "unable to write to file. Verify path and permissions";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/>
    <title>Test</title>
  </head>

  <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="TestForm"/>

      <p />
      Input anything: <input type="text" name="anything" value="Default"/> <br />
      <input type="submit" value="OK" name="submit"/>
    </form>
  </body>
</html>

sorry for taking so long to reply, I tried that and I get the following error:
Firefox can't find the file at /home/...

<?php echo $_SERVER['PHP_SELF']>

I am new to php and I dont know what this means I want form_output.txt to be created in a local directory, the same one my html file is in.

By the way, I get the form, the error appears when I press ok to submit

I'm not entirely sure why hielo decided to use $file=dirname(__FILE__) ."/form_output.txt"; on line 5 instead of just $file="form_output.txt"; but that should fix your problems though this also confuses me.

if( file_put_contents( $file, $data, FILE_APPEND) )
	{
		echo "unable to write to file. Verify path and permissions";
	}

If I'm reading that right, it'll echo that it was unable to write to the file if the file write succeeds? Though it is 1am so correct me if I'm wrong.

For your second question, what you're echoing there is the page name itself and has nothing to do with saving your text file.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="TestForm"/>

The way he uses it here is essentially the same thing as putting the page name into the action. If you go to view the source of the page, you'll see the full path to the page you're currently viewing. If I'm not mistaken, you can also accomplish this by leaving action="" entirely out of the equation and it'll submit the form to the same page anyway.

I have done that i.e making action="", it doesn't return the error but I don't get the file, I really don't know what to do anymore.

syd919, on your follow-up post (Sep 7th, 2010, 07:07 ) you have: <?php echo $_SERVER['PHP_SELF']> That is NOT what I posted. It should end with semi-colon followed ? and >:

<?php echo $_SERVER['PHP_SELF'];?>

As for Nyight's comment, about the if clause, he is right. I missed the leading ! symbol. It should be: if( !file_put_contents( $file, $data, FILE_APPEND) )

syd919, on your follow-up post (Sep 7th, 2010, 07:07 ) you have: <?php echo $_SERVER['PHP_SELF']> That is NOT what I posted. It should end with semi-colon followed ? and >:

<?php echo $_SERVER['PHP_SELF'];?>

As for Nyight's comment, about the if clause, he is right. I missed the leading ! symbol. It should be: [b]if( !file_put_contents( $file, $data, FILE_APPEND) )[/b]

Lol alright that makes a lot more sense :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.