Open the file in a new browser window after writing it (using fopen)

I am writing a simple ordering system where several numbers (filled inside the form) are written to another .php file (maybe also .html) using a function fopen. This works great, but after writing to a file, I want the browser to actually open this file, preferably in a new browser window. So my client can use this for printing, use as an invoice, etc.

Now I am still new to php-based and have no experience using it fopen. But everywhere I look for textbooks, etc., He said that he fopenopens (or, of course, writes) a file, but this is not as far as I knew. It seems to allow access to the specified file for writing and reading, rather than actually displaying the newly written page.

To avoid confusion: I do NOT want to open links, like the other questions here, in SO state.

My code is:

<form action="" method="post">
  <input type="text" id="amountTuna" name="numberTuna" value="0"/>
  <input type="text" id="amountCheese" name="numberCheese" value="0"/>
  <input name="send" id="send" type="submit" value="Post order" />
</form>

<?php
if (array_key_exists('send', $_POST)) { 
  $order = "order.php";
  $fh = fopen($order, 'w') or die("can't open file");//file handler

  fwrite($fh, "Tuna sandwiches: " . stripslashes($_POST['numberTuna']));
  fwrite($fh, "Cheese sandwiches: " . stripslashes($_POST['numberCheese']));

  $fh = fopen($factuur, 'r');
  $fileip = fread($fh, filesize($factuur));
  fclose($fh);
}
?>

Attempting to use different parameters fopen, such as 'w', 'r', 'r+', etc., it does not seem to make any difference. Removal fclose($fh)also does not matter.

+5
source share
3 answers

JS script, . , fclose($fh):

echo "<script>window.open($order, '_blank'); window.focus();</script>";
+5

, , .

, fopen , , (, Javascript) .

Google Javascript window.open().

Tom

+2

Since you just want to write to the page for printing .. you tried file_put_contents

Example

if (isset($_POST['send'])) {
    $file = "test.html";
    $data = "Tuna sandwiches: " . stripslashes($_POST['numberTuna']) . "<br>";
    $data .= "Cheese sandwiches: " . stripslashes($_POST['numberCheese']) . "<br>";
    touch($file);
    file_put_contents($file, $data);
}
+1
source

All Articles