PHP fwrite() doesn’t work

Im trying to take user input and write into a txt file then display that txt file on the page but it wont write. I dont have any errors either. If i try to run my code on repl.it it works fine. Code:

    <!DOCTYPE html>
<html>
<head>
  <title>Store form data in .txt file</title>
</head>
<body>
  <form method="post">
    Enter Your Text Here:<br>
    <input type="text" name="textdata"><br>
    <input type="submit" name="submit">
    
  </form>
    <iframe src="/data.txt"/>
</body>
</html>


<?php
              
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];

$fp = fopen('data.txt', 'a');

fwrite($fp, $data);
fclose($fp);
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Store form data in .txt file</title>
</head>
<body>
  <form method="post">
    Enter Your Text Here:<br>
    <input type="text" name="textdata"><br>
    <input type="submit" name="submit">
    
  </form>
    <iframe src="/data.txt"/>
</body>
</html>


<?php
              
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];

$fp = fopen('data.txt', 'a');

fwrite($fp, $data);
fclose($fp);
}
?>

Just the fact that you don’t see any errors doesn’t mean there aren’t any. Could you try enabling display errors in your control panel (in the Alter PHP Config menu) and then try to run the script again?

You could also capture the return value of the fwrite function. If it returns false, it means the data was not written correctly.

I think your issue here could be completely unrelated to PHP. I noticed there is no action="" in your <form> tag. Maybe try adding action="page url" to your <form> tag and see if that helps.

what do i put there? I didn’t need that on repl.it

ummmmm
Configure php directives Error, 9385928349285635786 please contact support

On which account and domain name do you get that error?

Or you can just edit the .htaccess file of your account and add the line:

php_flag display_errors on

any help? :slight_smile:

Put your exact page URL. So for example if the URL was https://example.com/mypage, then the <form> tag would look like this:

<form action="https://example.com/mypage" method="post">

Actually, if you don’t give it a value, it’ll send the form data to the page itself, so no need to put a url in it if your target is the same page.

From my experience, it can get weird if you don’t. And anyway its a good habit to include it.

1 Like

yes i put this, Still doesnt work

Did you turn display_errors on? If yes, turn the error reporting level up to max by adding this code at the top of your page:

<?php
error_reporting(E_ALL);
?>

It might display something that isn’t normally displayed.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.