PHP另存为PDF不起作用

时间:2016-10-11 06:03:29

标签: php pdf-generation save-as

美好的一天!

我有一个下拉菜单,用于选择员工编号并根据该特定选择显示表格数据。我保存为pdf代码,以pdf格式保存表格数据。它工作正常,但我的表列标题只显示不是值。

我有一个带有javascript文件的下拉列表,该文件被发送到另一个显示我的表格的文件。

以下是我所有文件的代码以及我迄今为止尝试过的内容。

下拉列表:

    <?php 
include("connect-db.php");
?>
<html>
<head>
<title>PHP insertion</title>
        <link rel="stylesheet" href="css/insert.css" />
        <link rel="stylesheet" href="css/navcss.css" />
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
        xmlhttp.send();
    }
}

/*function showAssessories(acc) {
    if (acc == "") {
        document.getElementById("txtHint2").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","getassessories.php?emp_id="+acc,true);
        xmlhttp.send();
    }
}*/
</script>
</head>
<body>
<div class="maindiv">
            <?php include("includes/head.php");?>
            <?php include("menu.php");?>
            <div class="form_div">


<form>
<div class="forscreen">
        <h1>Search  Records:</h1> 
        <p>You  may search by Employee Number</p>

<select name="employeenumber" class="input" onchange="showUser(this.value)">
  <option value="">Please Select Employee Number:</option>
  </div>
  <?php 
  $select_emp="SELECT distinct(emp_number) FROM employees"; 
  $run_emp=mysql_query($select_emp);
  while($row=mysql_fetch_array($run_emp)){
      $emp_id=$row['emp_id'];
      $first_name=$row['first_name'];
      $last_name=$row['last_name'];
      $emp_number=$row['emp_number'];
      $total_printers=$row['total_printers'];
      $total_scanners=$row['total_scanners'];
      $total_pc=$row['total_pc'];
      $total_phones=$row['total_phones'];
      $other_equips=$row['other_equips'];

?>
  <option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
  <?php } ?>
  </select>
  <html>


</form>
</div>
<br>
<div id="txtHint"></div>
<br>
<div id="txtHint2"></div>

</div>
<?php include 'includes/foot.php';?>
</body>

</html>

下拉列表功能:onchange =“showUser(this.value)”

function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
        xmlhttp.send();
    }
}

search_empnumber.php文件:

<!DOCTYPE html>
<html>
<form action="search_empnumber.php" method="POST">
<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />
</form>

<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
.link{ color:#00F; cursor:pointer;}
</style>
</head>
<body>

<?php
//$emp_id =($_REQUEST['emp_id']);
$emp_id =($_REQUEST['emp_id']);
$count=1;
include("connect-db.php");
 //$sql = "SELECT * from employees where department='".$department."'";
 //$sql = "select * from employees as pg inner join accessories as pd on pg.emp_number= pd.emp_number ";
//$result = mysql_query($sql) or die(mysql_error());

//$result = mysql_query($query) or die(mysql_error());
//$emp_id =($_POST['employeenumber']);
$sql="SELECT * FROM employees WHERE emp_number = '".$emp_id."'";
$result = mysql_query($sql) or die(mysql_error());

$count=1;
echo "<table>
<tr>
<th>S.No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Employee Number</th>
<th>Department</th>
<th>Email</th>
<th>Total Printers</th>
<th>Total Scanners</th>
<th>Total Pc</th>
<th>Total Phones</th>
<th>Other Equipments</th>

</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $count . "</td>";
    echo "<td>" . $row['first_name'] . "</td>";
    echo "<td>" . $row['last_name'] . "</td>";
    echo "<td>" . $row['emp_number'] . "</td>";
    echo "<td>" . $row['department'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['total_printers'] . "</td>";
    echo "<td>" . $row['total_scanners'] . "</td>";
    echo "<td>" . $row['total_pc'] . "</td>";
    echo "<td>" . $row['total_phones'] . "</td>";
    echo "<td>" . $row['other_equips'] . "</td>";

    echo "</tr>";
    $count++;

} 
echo "</table>";
echo '<div class="forscreen">';
      // echo '<br />';echo '<button class="btnPrint" type="submit" value="Submit" onclick="window.print()">Print</button>';
      //echo '<input type="submit" value="SAVE AS PDF" class="btnPDFsearch" name="submit"/>';
     // echo '<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />';

      echo '</div>';
echo '<div class="forscreen">';
      //echo '<br />';echo '<button class="btnPrevious" type="submit" value="Submit" onclick="history.back(); return false;">Previous Page</button>';

       echo '</div>';


?>

</body>
</html>
在search_empnumber的同一个文件中

PDF代码(我试过):

<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
include('connect-db.php');
?>
<?php
//if($_POST['submit']){
if (isset($_POST['submit'])){
    require_once 'includes/practice.php';
 $pdf->SetFont('times', '', 11);
  $tb1 = '</br></br>


                            <table cellspacing="0" cellpadding="5" border="1"> 

                                <tr style="background-color:#f7f7f7;color:#333; ">

                                    <td width="60">First Name</td>

                                    <td width="60">Last Name</td>

                                      <td width="80">Employee Number</td>

                                      <td width="80">Department</td>

                                    <td width="60">Email</td>

                                    <td width="80">Total Printers</td>
                                     <td width="80">Total Scanners</td>
                                      <td width="60">Total PCs</td>
                                      <td width="60">Total Phones</td>
                                       <td width="60">Other Equipments</td>



                                </tr>

                            </table>';


include('connect-db.php');
//$emp_id =($_POST['emp_id']);
$emp_id =($_POST['employeenumber']);
$result1= mysql_query("SELECT * FROM employees where emp_number = '".$emp_id."'");

        while($row = mysql_fetch_assoc($result1)){
            $tb2 = '<table cellspacing="0" cellpadding="5" border="1"> 
                <tr> 
                    <td width="60">' . $row['first_name'] . '</td>
                    <td width="60">' . $row['last_name'] . '</td>
                    <td width="80">'. $row['emp_number'] .'</td>
                    <td width="80">'.$row['department'].'</td>
                    <td width="60">'.$row['email'].'</td>
                    <td width="80">'.$row['total_printers'].'</td>
                    <td width="80">'.$row['total_scanners'].'</td>
                    <td width="60">'.$row['total_pc'].'</td>
                    <td width="60">'.$row['total_phones'].'</td>
                     <td width="60">'.$row['other_equips'].'</td>

                </tr>
            </table>';
            $tb1 = $tb1.$tb2;
        }


       $pdf->writeHTML($tb1, true, false, false, false, '');
       ob_end_clean();
      $pdf->Output('Report.pdf', 'D');

}

?>

不会添加文件,即practoce.php和pdf库,因为它们只是以定义的格式创建表格

2 个答案:

答案 0 :(得分:0)

我不知道哪个部分出了问题,但是下拉代码中出现了一些奇怪的事情:

   <select name="employeenumber" class="input" onchange="showUser(this.value)">
      <option value="">Please Select Employee Number:</option>
      </div>  <!-- ISSUE: Closed a div which was never opened. You're lucky the select box even shows anything-->
      <?php 
      $select_emp="SELECT distinct(emp_number) FROM employees"; 
      $run_emp=mysql_query($select_emp);

      //ISSUE: You loop through a lot of data, but then do nothing with it. With every pass of the loop, you simply change the variables, and nothing else. I think you meant to move your <option> into the loop
      while($row=mysql_fetch_array($run_emp)){

          //ISSUE: You only selected 'emp_number' in your query. Most of these array keys are never filled
          $emp_id=$row['emp_id'];  
          $first_name=$row['first_name'];
          $last_name=$row['last_name'];
          $emp_number=$row['emp_number'];  //This is the only one filled
          $total_printers=$row['total_printers'];
          $total_scanners=$row['total_scanners'];
          $total_pc=$row['total_pc'];
          $total_phones=$row['total_phones'];
          $other_equips=$row['other_equips'];

    ?>
      <!--ISSUE: Only the last $emp_number from the query is filled. The rest you kept overwriting in your loop-->
      <option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
      <?php } ?>
      </select>

答案 1 :(得分:0)

您正在混淆GETPOST。尝试以下一种(或两种):

1)在下拉代码中,将<form>替换为:

<form method="POST">

2)在表生成代码中,使用以下命令读取值:

$emp_id =($_REQUEST['employeenumber']);

还有:

if (isset($_REQUEST['submit'])){

要添加问题,请在发布的search_empnumber.php的第一个版本中,当发布的值实际位于$emp_id =($_REQUEST['emp_id']);时,您会回读$_REQUEST['employeenumber']

相关问题