title: Use php for Output Buffering and jQuery to send ob_get_contents
body: <p>I am trying to capture the contents of my php page using output buffering:</p>
  
  <pre><code>&lt;?php
  
  function connect() {
    $dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db("PDS", $dbh); 
    return $dbh;
  }
  
  session_start();
  
  if(isset($_SESSION['username'])){
    if(isset($_POST['entryId'])){
      //do something
      $dbh = connect();
      $ide = $_POST['entryId'];
      $usertab = $_POST['usertable'];
      $answertable = $usertab . "Answers";
      $entrytable = $usertab . "Entries";
      $query = mysql_query("SELECT e.date, q.questionNumber, q.question, q.sectionId, a.answer FROM $answertable a, Questions q, $entrytable e WHERE a.entryId = '$ide' AND a.questionId = q.questionId AND e.entryId = '$ide' ORDER BY q.questionNumber ASC;") or die("Error: " . mysql_error());
  
      if($query){
        //set variables      
        $sectionOne = array();      
        while($row=mysql_fetch_assoc($query)){
      $date = $row['date'];
      $sectionOne[] = $row;   
        }
      }else{
        //error - sql failed
      }
    } 
  ?&gt;
  
  &lt;?php
    ob_start();
  ?&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
     &lt;head&gt;
     &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
     &lt;script src = "jQuery.js"&gt;&lt;/script&gt;
     &lt;script&gt;
     $(document).ready(function(){
         $("#export").click(function(e){
         //post to html2pdfconverter.php
         $("#link").val("&lt;?php echo(ob_get_contents()); ?&gt;"); //THIS DOESN'T WORK
         $("#nm").val("Entry Report.pdf");       
         $("form#sendanswers").submit();
         }); 
     });
    &lt;/script&gt;
    &lt;title&gt;Personal Diary System - Entry Report - &lt;?php echo($date); ?&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;Entry Report - &lt;?php echo($date); ?&gt;&lt;/h1&gt;    
    &lt;div id = "buttons"&gt;
    &lt;form id = "sendanswers" name = "sendanswers" action="html2pdfconverter.php" method="post"&gt;
    &lt;input type = "hidden" name = "link" id = "link" value = ""&gt;
    &lt;input type = "hidden" name = "nm" id = "nm" value = ""&gt;
    &lt;input type = "button" name = "export" id = "export" value = "Export As PDF"/&gt;
    &lt;/form&gt;                        
    &lt;/div&gt;                         
    &lt;h3&gt;Biological Information&lt;/h3&gt;
    &lt;?php
       echo('&lt;p&gt;');                         
        $i = 0;                         
        foreach($sectionOne as &amp;$value){
      if($i == 1 || $i == 3){
        $image = "assets/urine".$i.".png";
        echo("&lt;br/&gt;");
        echo($value['question']." &lt;br/&gt; "."&lt;img src = \"$image\"/&gt;");
        echo("&lt;br/&gt;");
      }else{
        echo($value['question'].' : '.$value['answer']);
      }
      echo("&lt;br/&gt;");
      $i++;
        }
       echo('&lt;/p&gt;');      
    ?&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php   
  }
  $contents = ob_get_contents(); //THIS WORKS    
  ob_end();
  ?&gt;
  </code></pre>
  
  <p>I assign the contents of <code>ob</code> to <code>$contents</code> using <code>ob_get_contents();</code> This works, and echoing $contents duplicates the html page.</p>
  
  <p>However, in my jQuery, I am trying to assign this to a hidden text field ('link') using:</p>
  
  <pre><code>$("#link").val("&lt;?php echo($contents); ?&gt;");
  </code></pre>
  
  <p>This doesn't work however..And I have a feeling its because I am accessing $contents too eraly but not too sure...any ideas?</p>