Page 1 of 1

php form to .txt with fwrite?

Posted: Sun Oct 17, 2010 12:37 am
by pstar99
Hi,
I'm a PHP newbie and trying to make a script for the last few days that is giving me a great headache ;-(
Currently I have an input.php page with a simple form (see input.php below).
Once the submit button is clicked it sends me over to output.php where it shows the input values embedded in html code (see output.php below).

What I'm hoping to accomplish is:
Once the input form is filled and the submit button is clicked it saves the entered values and embedded within the html code as a .txt file on my server (just like output.php below but with the actual values instead of echo) ... and next time I use input.php it updates/overwrites the .txt file with the new values embedded within the html code.

I've experimented with fwrite "w", but somehow I can't get it to work.
Also, if the input values can be saved straight to a .txt file and embedded within the html code, would I still need output.php?
Could someone please help me with this or point me in the right direction?

Thank you so much!!! ;-)

Current Input (input.php):

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Input Output Test File</title>
</head>
<body>
<h2>Data Input</h2>
<form action="output.php" method="post">
<input type="text" name="city" /> City<br />
<input type="text" name="col" /> Color<br />
<input type="text" name="pro" /> Product ID<br />
<input type="submit" />
</form>
</body>
</html>
Current Output (output.php):

Code: Select all

<div class="fea">
<p><a href="http://www.domain.com/products.php?pro=<?php echo $_POST["pro"]; ?>">
<img src="http://www.domain.com/images/<?php echo $_POST["pro"]; ?>.jpg" height="75" width="100" /></a>
<strong>City: <?php echo $_POST["city"]; ?></strong><br />
Color: <?php echo $_POST["col"]; ?></p>
<p><a href="http://www.domain.com/products.php?pro=<?php echo $_POST["pro"]; ?>">Details</a></p>
</div>

Re: php form to .txt with fwrite?

Posted: Sun Oct 17, 2010 2:38 am
by requinix
The form has action=output.php so yes, you still need output.php.

What did you try with writing to a file? Did you read the manual page for fwrite and realize you need fopen() too, or are you trying to give fwrite() itself the filename?

Re: php form to .txt with fwrite?

Posted: Sun Oct 17, 2010 2:53 am
by Bind
put output data in a string variable using concatenation, then save to file, and output the string to the browser upon successful write ...

since your string is a static simple write and you need no dynamic data exchanges or data seeking, I would recommend file_put_contents() over fopen(), fwrite(), and fclose() for less server resources being consumed.

example of output.php:

Code: Select all

<?php
$file_name_to_write = 'myfile.txt'; # this is the file name you want o write to the server
if($_POST['submit'])
    {
        $output ='<div class="fea">
<p><a href="http://www.domain.com/products.php?pro='.$_POST["pro"].'">
<img src="http://www.domain.com/images/'.$_POST["pro"].'.jpg" height="75" width="100" /></a>
<strong>City: '.$_POST["city"].'</strong><br />
Color: '.$_POST["col"].'</p>
<p><a href="http://www.domain.com/products.php?pro='.$_POST["pro"].'">Details</a></p>
</div>';
        if(file_put_contents ($file_name_to_write,$output, LOCK_EX))
            {
                echo $output;
            }
        else
            {
                echo 'error writing data to file.';
            }
    }
?>
the above code overwrites the data in the file if it exists, but if you want to append to, instead of overwrite, change to the following line:

Code: Select all

if(file_put_contents ($file_name_to_write,$output, FILE_APPEND | LOCK_EX))]
Make sure write permissions exist on the directory and file else you may get a 'permission denied' error from php.

closer but not there yet ... Please Help

Posted: Sun Oct 17, 2010 3:56 pm
by pstar99
Thanks for the advice. I tried to use the code you provided, but it didn't write anyhting to the txt file (although permission was set).
I found something really close to what I wanted to do:

input.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Input Output Test File</title>
</head>
<body>
<h2>Data Input</h2>
<?php 
$data ='<div class="fea">
<p><a href="http://www.domain.com/products.php?pro='.$_POST["pro"].'"><img src="http://www.domain.com/images/'.$_POST["pro"].'.jpg" height="75" width="100" /></a>
<strong>City: '.$_POST["city"].'</strong><br />
'.$_POST["col"].' Color</p>
<p><a href="http://www.domain.com/products.php?pro='.$_POST["pro"].'">Details</a></p>
</div>';
file_put_contents("output.txt", $data); 
?>
<form method="post">
<input type="text" name="pro" /> Product ID<br />
<input type="text" name="city" /> City<br />
<input type="text" name="color" /> Color<br />
<input type="submit" name="submit" value="Submit"/> 
</form>
</body>
</html>
It works, BUT ... as soon as I go back to the input.php page and without entering any data or clicking submit it deletes the values that were previously entered in the input fields on output.txt and only keeps the html code around it. As long as I stay away from input.php the input field data is being kept.

How can I adjust this?

Any help is greatly appreciated. Thank you.

Re: php form to .txt with fwrite?

Posted: Sun Oct 17, 2010 8:13 pm
by requinix
Make it so that the code only executes if the form was submitted. Example: look in $_POST for the button name.