PHP: How to open a new window in JS and echo from PHP into it

1.7k views Asked by At

for debugging I reasons I use very often PHP function var_dump()

<pre>
  <?php
    var_dump($myVariablesArray);
  ?>
</pre>

but I need to output its contents (and something more I use for my debugging) into a new popup window.

There are several examples about opening a new JS window, but I cannot find anything helping me opening a new window and printing from PHP into it, all this automatically when the page I'm debugging is loaded.

Any hint?

2

There are 2 answers

0
Thomas Martin Klein On

you could var_export() instead maybe. And then append it to the beginning of a txt on the server. Then you can have a little script in the second window that refreshes every 5 seconds, and shows that txt file. Much like a log.

http://www.php.net/manual/en/function.var-export.php

pro: you can keep the txt between runs, and save the outputs.

con: the format is a bit different than var_dump()

0
opoloko On

I solved the problem thanks to suggestion on Add content to a new open window

On the page I want to debug, where I need to, I add following code (if session is not started I need to add "session_start();" ):

<?php
$_SESSION['varsLog'] = "<pre>".htmlspecialchars(print_r($myVariablesArray))."</pre>\n";
?>
<script type="text/javascript">
$(document).ready(function () {
var OpenWindow = window.open("/empty.html", "phpLog", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1400,height=640');
});
</script>

and then I have the empty.html page on the root of my domain:

<?php session_start(); if (!isset($_SESSION['varsLog'])) { exit(1); } ?>
<html><body>
<?php
echo date(DATE_RFC822); 
$refer = $_SERVER['HTTP_REFERER'];
echo "<br><br>Variables from <a href=".$refer.">".$refer."</a><br><br>";
echo $_SESSION['varsLog'];
unset($_SESSION['varsLog']);
?>
</body></html>

This way I can add the first code snippet to any page I need to debug, every time I load those pages the new window I previously opened will be just updated with the variables from the last page loaded, with a useful referring url and time stamp to be sure.

Thanks for help to everyone!