0

I want to print a HTML table using some PHP data, in a separate window when I press a button. What is the best way to do it?

<table border="1"> 
    <tr>
        <td>MODEL</td>
        <td><?php echo $modelNo ?></td>
        <td>MODEL</td>
        <td><?php echo $modelNo ?></td>
    </tr>
    <tr>
        <td>QTY</td>
        <td><?php echo $box ?></td>
        <td>QTY</td>
        <td><?php echo $box ?></td>  
    </tr>
</table>
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
HaK
  • 141
  • 2
  • 4
  • 13
  • 1
    The question here is not about PHP, but HTML. Your PHP program doesn't print anywhere. It creates output that the browser requests. The trick here (in the answer below) is to have the HTML to open a new link in another window. – Andy Lester Mar 06 '13 at 03:04

4 Answers4

1

Have the button open the new window, and set its location to the PHP file that prints the table.

Something as simple as this could work:

<a href="show_table.php" target="_blank">Click</a>

(You can optionally style this link to look like a button)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You will need to have this table returnable from a PHP script, or available somewhere (I assume you already do).

Here is a purely html method:

<a href="tabledata.php" target="_blank">Open Table!</a>

Here is a javascript method:

<script type="text/javascript">
<!--
function myPopup() {
window.open( "http://path.to.my.page.com/mypage.php" )
}
//-->
</script>
</head>
<body>
<form>
<input type="button" onClick="myPopup()" value="Open table!">
</form>
<p onClick="myPopup()">CLICK ME TOO!</p>
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
0

You can also have the html in a hidden input and use a form submit to the other page and retrieve it from there with $_POST like assuming $table_data is your html:

<form action="new_window.php">    
<input type="hidden" value="$table_data" name="data">
<input type="submit" value="Open Table"> 
</form>

Then in new_window.php:

if(isset($_POST['data'])){
echo $_POST['data'];
}
bruhbruh
  • 313
  • 1
  • 14
0

You can also create the entire solution in javascript by creating a new window and by writing the content in the window.

see the basic approach writing in a new window here: http://www.w3schools.com/jsref/met_win_open.asp and Print the contents of a DIV

You would have to add an id attribute to your table

Then playing with DOM functions such as : https://developer.mozilla.org/en-US/docs/DOM/document.getElementById and https://developer.mozilla.org/en/docs/DOM/element.innerHTML

You will have to wrap your table into a html. The main advantage of this method is that it can be called later on any table without the need to add a line of php.

Community
  • 1
  • 1
Bertrand
  • 388
  • 4
  • 13