PHP保存为PDF不工作


PHP Save as PDF not working

大家好!

我有一个下拉菜单,选择员工编号,并显示表数据的基础上,特定的选择。我保存为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>

PDF代码(我尝试过)在search_empnumber:

<?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');
}
?>

没有添加practice .php和pdf库,因为它们只是在创建定义格式的表

你把GETPOST弄混了。尝试以下一种(或两种)方法:

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

<form method="POST">

2)在表生成代码中,用:

读取值
$emp_id =($_REQUEST['employeenumber']);

也:

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

为了增加您的问题,在您发布的search_empnumber.php的第一个版本中,当发布的值实际上在$_REQUEST['employeenumber']中时,您回读$emp_id =($_REQUEST['emp_id']);

我不知道你哪部分出了问题,但下拉列表代码中有一些奇怪的事情:

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