简体   繁体   English

使用php进行输出缓冲和jQuery发送ob_get_contents

[英]Use php for Output Buffering and jQuery to send ob_get_contents

I am trying to capture the contents of my php page using output buffering: 我正在尝试使用输出缓冲捕获我的php页面的内容:

<?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
    }
  } 
?>

<?php
  ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <script src = "jQuery.js"></script>
   <script>
   $(document).ready(function(){
       $("#export").click(function(e){
       //post to html2pdfconverter.php
       $("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
       $("#nm").val("Entry Report.pdf");       
       $("form#sendanswers").submit();
       }); 
   });
  </script>
  <title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
  </head>
  <body>
  <h1>Entry Report - <?php echo($date); ?></h1>    
  <div id = "buttons">
  <form id = "sendanswers" name = "sendanswers" action="html2pdfconverter.php" method="post">
  <input type = "hidden" name = "link" id = "link" value = "">
  <input type = "hidden" name = "nm" id = "nm" value = "">
  <input type = "button" name = "export" id = "export" value = "Export As PDF"/>
  </form>                        
  </div>                         
  <h3>Biological Information</h3>
  <?php
     echo('<p>');                         
      $i = 0;                         
      foreach($sectionOne as &$value){
    if($i == 1 || $i == 3){
      $image = "assets/urine".$i.".png";
      echo("<br/>");
      echo($value['question']." <br/> "."<img src = \"$image\"/>");
      echo("<br/>");
    }else{
      echo($value['question'].' : '.$value['answer']);
    }
    echo("<br/>");
    $i++;
      }
     echo('</p>');      
  ?>
  </body>
</html>
<?php   
}
$contents = ob_get_contents(); //THIS WORKS    
ob_end();
?>

I assign the contents of ob to $contents using ob_get_contents(); 我使用ob_get_contents();ob的内容分配给$contents ob_get_contents(); This works, and echoing $contents duplicates the html page. 这有效,并且回显$ contents复制了html页面。

However, in my jQuery, I am trying to assign this to a hidden text field ('link') using: 但是,在我的jQuery中,我尝试使用以下方法将其分配给隐藏的文本字段(“链接”):

$("#link").val("<?php echo($contents); ?>");

This doesn't work however..And I have a feeling its because I am accessing $contents too eraly but not too sure...any ideas? 但是,这是行不通的。我有一种感觉,因为我太过分地但不太确定地访问$ contents ...有什么想法吗?

   $("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK

at the point you do that ob_get_contents call, you've only output about 10 lines of javascript and html. 在您执行ob_get_contents调用的那一刻,您仅输出了大约10行javascript和html。 PHP will NOT reach back in time and magically fill in the rest of the document where you do this ob_get_contents(). PHP 不能及时伸出手奇迹般地填补了文档的其余部分,你这样做ob_get_contents()。

You're basically ripping the page out of the laser printer the moment the page starts emerging, while the printer is still printing the bottom half of the page. 当页面开始出现时,您基本上是在将页面从激光打印机中撕下,而打印机仍在打印页面的下半部分。

I fail to see why you want to embed the contents of your page into an input field. 我看不到您为什么要将页面内容嵌入到输入字段中。 If you want to somehow cache the page's content in an input field, you can just use JS to grab the .innerHTML of $('body') . 如果您想以某种方式在输入字段中缓存页面的内容,则可以使用JS来获取$('body')的.innerHTML。

Well, you have two problems. 好吧,您有两个问题。

The first is what you suspect. 首先是您所怀疑的。 You can't access that stuff until later. 您稍后才能访问这些内容。 The second problem which you may not realize is that you will have quoting issues in JavaScript even if you manage to find a way to reorder this and make it work. 您可能没有意识到的第二个问题是,即使您设法找到一种重新排序并使之起作用的方法,也将在JavaScript中出现引用问题。 It's recursive, in a bad way. 它是递归的,以一种不好的方式。

What you should do instead is change your $('#export').click handler to do an Ajax call, render the HTML you need to appear in the link on the server in a separate PHP script (no output buffering necessary) and then have your code inject the result of that call into the page the way you're trying to do in your click handler now. 相反,您应该做的是更改$('#export').click处理程序以进行Ajax调用,在单独的PHP脚本中渲染需要显示在服务器链接中的HTML(无需输出缓冲),然后让您的代码立即按照您在点击处理程序中尝试的方式将该调用的结果注入页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM