17

I've searched around this site for an answer but couldnt find any.

I have a form and I'd like to get the contents of the input written into a txt file. To make it simple I just wrote a simple form and a script but it keeps getting me a blank page. Here is what I got

<html>
<head>
    <title></title>
</head>
<body>
    <form>
        <form action="myprocessingscript.php" method="post">
        <input name="field1" type="text" />
        <input name="field2" type="text" />
        <input type="submit" name="submit" value="Save Data">
    </form>
    <a href='data.txt'>Text file</a>
</body>

and here is my PHP file

<?php
$txt = "data.txt"; 
$fh = fopen($txt, 'w+'); 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
   $txt=$_POST['field1'].' - '.$_POST['field2']; 
   file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt 
   exit();
}
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
    ?>
user2094841
  • 205
  • 1
  • 2
  • 4
  • 1
    Does `myprocessingscript.php` produce any further output? If your second snippet is the whole PHP script then a blank page is what you should get as you are not generating any output. – acme Feb 21 '13 at 09:46
  • Also it is normal that you get a blank page, you are no `echo()`ing anything to the client. – Floby Feb 21 '13 at 10:13

5 Answers5

46

Your form should look like this :

<form action="myprocessingscript.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

and the PHP

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
    $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

I wrote to /tmp/mydata.txt because this way I know exactly where it is. using data.txt writes to that file in the current working directory which I know nothing of in your example.

file_put_contents opens, writes and closes files for you. Don't mess with it.

Further reading: file_put_contents

Floby
  • 2,306
  • 17
  • 15
  • I changed it back to 'data.txt' but the rest of it is perfect – user2094841 Feb 21 '13 at 10:14
  • On a shared host, where you don't have access to /tmp, where would you place the mydata.txt? My directory structure is ~/www-root/ – cryptic0 Mar 20 '16 at 16:16
  • Really appreciate the time you spent here, Floby. This is really elegant solution and allowed me to ditch a ton of scripts required by 3rd party form processors such as are available with WordPress plugins. :) – Nathan Mar 03 '17 at 20:06
5

The problems you have are because of the extra <form> you have, that your data goes in GET method, and you are accessing the data in PHP using POST.

<body>
<!--<form>-->
    <form action="myprocessingscript.php" method="POST">
Community
  • 1
  • 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

A possible solution:

<?php
$txt = "data.txt"; 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
    $fh = fopen($txt, 'a'); 
    $txt=$_POST['field1'].' - '.$_POST['field2']; 
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
}
?>

You were closing the script before close de file.

Lobo
  • 4,001
  • 8
  • 37
  • 67
0

If you use file_put_contents you don't need to do a fopen -> fwrite -> fclose, the file_put_contents does all that for you. You should also check if the webserver has write rights in the directory where you are trying to write your "data.txt" file.

Depending on your PHP version (if it's old) you might not have the file_get/put_contents functions. Check your webserver log to see if any error appeared when you executed the script.

Naryl
  • 1,878
  • 1
  • 10
  • 12
0

use fwrite() instead of file_put_contents()

s.lenders
  • 1,119
  • 6
  • 21