-1

My company uses an invoice software I've made. It's a simple page that copies input from fields, checkboxes and some dynamic table with Javascript into a new page with A4 CSS so it can be printed or sent via mail as PDF.

Now I have to start on the second feature: open an invoice. I have thought of one way I could make this.

When a new invoice is made and input is copied into the other page, to also copy all the HTML from the form in which all the inputs are into a text file with PHP - and later when a user "opens" an invoice, it just opens the file which just copies all the HTML code.

I don't know how to copy part of the code into a text file. If you also know an easier, better way please tell.

halfer
  • 19,824
  • 17
  • 99
  • 186
David Dr.
  • 87
  • 9

1 Answers1

0

Use output buffering.

Before you render anything out to the browser do

ob_start();

after you do your rendering get the html content.

$html = ob_get_contents();

then to send it to the browser do.

ob_flush();

Then with the $html you can save to file, database or whatever works for your use case

Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • This will copy all html from the page? So then I can copy all from $html to a text file? – David Dr. Jun 11 '13 at 06:12
  • That's what it does. I use it a lot for confirmation emails. Show on screen and use in mail body. – Orangepill Jun 11 '13 at 06:13
  • Sounds good. Last question -- ob_start(); should be in the start of the page in which the invoice is made or the page where the inputs are? – David Dr. Jun 11 '13 at 06:15
  • ob_start just starts capturing the data so put it before you start outputting what you want to save and do the ob_get_contents() right after you output it. If you wanted the entire html page the ob_start should be before any echo statements or html. – Orangepill Jun 11 '13 at 06:18
  • For example if you wanted to capture a certain table in your code you would do: `ob_start(); echo ""; /* everything else to build your table */; echo "
    "; $table = ob_get_contents();
    – Orangepill Jun 11 '13 at 06:20