0

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

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Nyight
0

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>
0

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.

0

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

0

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.

Edited by Nyight: n/a

0

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.

0

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) )

0

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.