Search code examples
javascriptphpfwrite

I am Forced To Refresh File (html) After fwrite to Show Changes


General View:

I am writing a simple html table in a file on my server (http://example.com/stock_order.html), then returning file url to Javascript using ajax to open in new tab.this is my code:

PHP:

   $newfile = fopen($_SERVER['DOCUMENT_ROOT']."/stock_order.html","w+") or    die("Unable to open file!");
    $txt ='';
    $txt .='Here i put table data'; 
    fwrite($newfile, $txt);
    fclose($newfile);
    $link = "http://www.webber.solutions/stock_order.html";
    echo $link;

JS:

	 $.ajax({
	url: "index.php?route=sale/print/printStock&token=<?php echo $token; ?>",
	type: "post",
	data: {ids:ids},
	cache: false,
	}).done(function( data ) {
       // using url from php i open a new tab here
	window.open(data , '_blank');
	});

Problem: data is being written correctly in file, but with some delay, meaning that when redirected to html file, file content is the old data, newly written data is only shown when i refresh page manually.

I have tried to put delay of 1-2 seconds after fwrite,still no change.

is this server related? or there is something wrong with my code? Please Help


Solution

  • it's most likely a browser cache, not the file being "old" try to change the url as such:

    echo "http://www.webber.solutions/stock_order.html?t=".time();
    

    or something similar to avoid cache