窗口打开时停止在php中重写

I have a popup window that opens when I want a person to fill in a textbox. The textbox is then transferred to a .txt file on the server. My problem is that once the popup window is opened, the text on the .txt file is overwritten as a blank file until the user adds their own text.

I would like to figure out what I'm doing wrong or try to find a way to correct this issue.

This is my php:

<?php
date_default_timezone_set('DST');

// Open the text file and prepare to write (will be blank if no text is entered.)
$f = fopen("blog.txt", "w");

// Write text
fwrite($f, $_POST["textblock"] . " -- " . date("l jS \of F Y", time()) . " "); 

// Close the text file
fclose($f);

?>

How do I prevent it from blanking out when I open the blog.txt file? I'd like to keep the text that was on there before a rewrite.

what is happening your case is that you are overwrite old file and create new file.if you want to append new text to the file without losing old data follow the example below :

$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Your new data 
";
fwrite($fh, $stringData);
fclose($fh);

It overwrites the existing file content because of using below code:

$f = fopen("blog.txt", "w");

w mode which erases the existing content and adds the new content into the file. File pointer starts at the beginning of the file.

So you can use a instead of w, which preserves the existing content and also adds the new content to the file. Here, the file pointer starts at the end of the file.

Try this code:

<?php
    date_default_timezone_set('DST');
    $f = fopen("blog.txt", "a") or die("Can't open file");
    fwrite($f, $_POST["textblock"] . " -- " . date("l jS \of F Y", time()) . " " . PHP_EOL); 
    fclose($f);
?>