GauthamK
GauthamK

Reputation: 188

How to get the HTML page loaded and ready before i get the code for the current page through php?

I have two pages here. I use the first page(mpdfData.php) to get the data. And I send the data to the second page(mpdfPage.php) to create a HTML page that includes the data I have sent from the first page.

The idea is to form the second page and use it to print a PDF by converting it from HTML to PDF via the PHP code in the first page.

The problem I am facing is, when I get the HTML code of the second page back to the first, the values I have sent from page one do get displayed, instead the code itself <?php echo $user?> & <?php echo $pass?> get displayed in the target PDF.

How do I over come this?

<!-- mpdfData.php -->
<?php
$html="";
if (isset($_POST['submit'])) {
    $user=$_POST['userName'];
    $pass=$_POST["password"]; 
    ?><div hidden><?php
    include 'mpdfPage.php'; 
    ?><div><?php

}

if ($html !== '') {
require_once __DIR__ . '/vendor/autoload.php';
    // Create an instance of the class:
    $mpdf = new \Mpdf\Mpdf();

    // Write some HTML code:
    $mpdf->WriteHTML($html);

    // Output a PDF file directly to the browser
    $mpdf->Output();
}

?>

<html>
<head>
<title>User Login</title>
<link rel="stylesheet" type="text/css" href="styles1.css" />
</head>
<body>
<form name="frmUser" method="post" action="">
  <div class="message1"><h2>PDF PAGE:</h2></div>
    <table border="0" cellpadding="10" cellspacing="1" width="500" align="center" class="tblLogin">
      <tr class="tableheader">
      <td align="center" colspan="2">Enter PDF Details</td>
      </tr>
      <tr class="tablerow">
      <td>
      <input type="text" name="userName" placeholder="User Name" class="login-input"></td>
      </tr>
      <tr class="tablerow">
      <td>
      <input type="password" name="password" placeholder="Password" class="login-input"></td>
      </tr>
      <tr class="tableheader">
      <td align="center" colspan="2">
        <input type="submit" name="submit" value="Print Pdf" class="btnSubmit"></td>
       </tr>
    </table>

</form>
</body>
</html>
<!-- mpdfPage.php -->
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>
<form name="table" method="post" action="">
<table>

  <tr>
    <th>User Name</th>
    <th>Password</th>
  </tr>
  <tr>
    <td><?php echo $user?></td>
    <td><?php echo $pass?></td>
  </tr>

</table>
</form>
</body>
</html>

<?php
$html = file_get_contents(__FILE__);
?>

Upvotes: 0

Views: 99

Answers (1)

Ropali Munshi
Ropali Munshi

Reputation: 3006

The problem you are getting because of the file_get_contents(__FILE__) don't execute the PHP code and it will provide raw contents of the file. You can achieve that using the following way.

<?php ob_start(); ?>

<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>
<form name="table" method="post" action="">
<table>

  <tr>
    <th>User Name</th>
    <th>Password</th>
  </tr>
  <tr>
    <td><?php echo $user?></td>
    <td><?php echo $pass?></td>
  </tr>

</table>
</form>
</body>
</html>

<?php $html = ob_get_clean(); ?>

Upvotes: 1

Related Questions