2013-07-31 27 views
6

私はphp mysqlのフォームで作業しています。window.print()でページの特定の部分を印刷する方法

<span style="float:right;"><a href="javascript:window.print()" type="button" class="btn">PRINT</a></span> 

それ:私は同じようにPRINTボタンを作った理由だけでテーブル構造がthatsの私のページにフォームがあると私は印刷したいformat.nowその下に私は、テーブル内のそのフォームのデータを示しますフォームフィールドとテーブルを使ってページ全体を印刷します。テーブルを印刷したいだけです。ここ は、フォームとテーブルと私のページ全体のデザインです:

<html> 
<head></head> 
<body> 
<div class="container"> 
<form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data"> 
    <?php if($_GET[id]){?> 


<fieldset> 
    <legend>Add Company</legend> 
    <div class="control-group"> 
    <label class="control-label">Company Name</label> 
    <div class="controls"> 
    <input type="text" name="company" id="company" value="<?php echo $selup['company']?>"> 
    </div> 
    </div> 
<div class="control-group">another field</div> 
<div class="control-group">another field</div> 
<div class="control-group"> 
    <div class="controls"> 
    <button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button> 
    </div> 
    </div> 
<table class="table table-bordered"> 
      <tbody> 
      <tr> 
       <td>S.No.</td> 
       <td>Company Name</td> 
       <td>Type</td> 
       <td>Action</td> 
      </tr> 
       <?php 
       // to print the records 
       $select = "select * from company where type='Miscellaneous'"; 
       $query1 = mysql_query($select); 
       while($value = mysql_fetch_array($query1)){ ?> 

       <tr> 
       <td><?php echo $value[id];?></td> 
       <td><?php echo $value[company ];?></td> 
       <td><?php echo $value[type];?></td> 
       <!--<td>&nbsp;</td>--> 
       <?php /*?><td><?php echo $value[amount];?></td>    
       <td><?php echo $value[date];?></td><?php */?> 
       <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $value[id];?>&cmd=edit"><i class="icon-edit"></i></a> 
       <a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $value[id];?>&cmd=delete" onclick="return confirm('Are you sure you want to delete <?php echo $value[customer];?>?')"><i class="icon-trash"></i></a></td> 


       </tr><?php }?> 
     </tbody> 
     </table> 
</fieldset> 
<form> 
</div> 
</body> 

は、だから、僕は、テーブル全体ではなく、ページを印刷したいです。

+0

可能性の重複:http://stackoverflow.com/questions/11738380/why-does-it-print-only-one-time-please-give-me-solution/11738592#11738592 –

+0

言いたいことは、フォームタグは閉じられていないということです。 – IGRACH

+0

この回答をお試しください:http://stackoverflow.com/a/26114555/3944217 – edCoder

答えて

9

使用CSS:

@media print { 
    .control-group { 
     display: none; 
    } 
} 
+0

私はそれをやったが、私のために働いていないいくつかの理由.. :(私は私が同じページ上のものをaal持っていると述べたように – Montiyago

+0

ちょっと最後私はあなたの貴重な助けの仲間のためにそれをやった。 – Montiyago

2

一つかのうソリューション、おそらくない最良のオプションは:

1. Open a new window with JS 
2. Copy the whole table into the new window (with jQuery for example) 
3. Print the new window 
4. Close the window 

確かにそれは点滅効果を持っていますが、それは動作します。あなたが印刷したくない要素を非表示にする

3

あなたは@media印刷と同じにした(印刷-CSSを作ることができ、私はそれがきれいだと思います、そしてあなたあなたがそれを必要とする場合)、ジャバスクリプトを経由して含まれてCSSを無効にすることができます

<link rel="stylesheet" type="text/css" href="print.css" media="print" /> 

をそのCSSでは、例えば、印刷されませすべきすべての要素を隠す:

.wrapper, .header {display: none;} 
+0

あなたの貴重な助けをありがとうが何らかの理由で私のために働いていない。 – Montiyago

+0

あなたの貴重な助けを借りてありがとう..私はそれをやった。 – Montiyago

3
function printContent(el){ 
    var restorepage = document.body.innerHTML; 
    var printcontent = document.getElementById(el).innerHTML; 
    document.body.innerHTML = printcontent; 
    window.print(); 
    document.body.innerHTML = restorepage; 
} 
<!DOCTYPE html> 
<html> 
<head> 

</head> 
<body> 
<h1>My page</h1> 
<div id="div1">DIV 1 content...</div> 
<button onclick="printContent('div1')">Print Content</button> 
<div id="div2">DIV 2 content...</div> 
<button onclick="printContent('div2')">Print Content</button> 
<p id="p1">Paragraph 1 content...</p> 
<button onclick="printContent('p1')">Print Content</button> 
</body> 
</html> 
関連する問題