如何在没有新窗口打开的情况下将变量传递给php?

So i have a page where the visitor inputs data into a text area and i want that data to be saved to a file.

All is working fine but when the data is passed to the php script it opens a new window and i don't want that. I want the data to be passed without opening a new window or without the page having to refresh.

My javascript code to pass the data to php:

window.open('post.php?' + data, target='_blank');

My php script that receives the data and writes it to a file:

<?php
$handle = fopen("data.txt", "a");
foreach($_GET as $variable => $value) {
   fwrite($handle, $variable);
   fwrite($handle, $value);
   fwrite($handle, "
");
}
fwrite($handle, "
");
fclose($handle);
exit;
?> 

Thank you!

You need to do it with ajax:

       $.get("post.php?" + data, function(data){
           console.log(data);
       });

P.S: Remember to include jQuery.

</div>

Found my answer:

window.open('post.php?' + data, target='_blank').close();

This closes the window after opening it.

     $(function(){
       $.get("post.php?" + data, function(data){
           
       });
    });

This worked for me! Thanks!

</div>