views:

58

answers:

2

Hi Guys,

I'm doing a project in codeginiter these days, i want to print just the text area when the print button is clicked? can you guys help me to achieve this?

regards. Rangana

+2  A: 

Hi Rangana, codeigniter is an excellent framework, but unfortunately doesn't quite apply in this case because php is a server side scripting language and printing is done clientside. For printing a given area you'll want to use some javascript, specifying the div you want to print out. I would recommend the jQuery library as it has a print plugin I've used in a few of my projects. So you would use php to define code like this

<div id="content"> ... </div>

<div id="print_button">Print</div>

<script type="text/javascript">
  // javascript goes here, if you were using jQuery 
  // with the PrintArea pluginyou might do...
  $("div#print_button").click(function(){
    $("div#content").printArea([options]);
  });
</script>

Which would then execute client side when the print button is pressed.

Check out jquery here http://jquery.com/

There's also the vanilla window.print() function with setting up a print stylesheet if you don't want to use jQuery.

clumsyfingers
thank you very much for your valuable answer, will definitely checkout jquery too... thanks once again!
ranganaMIT
+1  A: 

If you want to define certain elements to be printed only you need to create a separate css stylesheet:

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

Every element which should not be printed can be hidden using display: none;. See this page for more infomation.

Nick
thank you for the help Nick, i'm referring your link now!thank you once again!
ranganaMIT