弹出说"文件正在下载"但什么也没发生

时间:2016-11-08 18:05:27

标签: php html

基本上我有一个网站,让用户输入两个日期。当他们单击“提交”时,将显示一个表,该表仅显示这两个日期之间的数据。

当我点击下载按钮时,弹出窗口显示该文件正在下载但没有任何反应。

我已经用Google搜索并查看了这个问题的几个答案,但是虽然我没有收到任何错误,但文件仍然无法下载。我应该尝试不同的方法吗?

这是我的PHP代码:

<?php

    session_start();
    include_once("Connection.php");
    $StartDate = $_POST['startDate'];
    $EndDate = $_POST['endDate'];
    $InstructorID = $_SESSION["InstructorID"];


    $sql = "SELECT Instructors.InstructorName, Class.ClassName, Class.ClassType, CLASS_STUDENT.Semester, CLASS_STUDENT.Year, Students.StudentName, TUTOR_STUDENT.ClassID, TUTOR_STUDENT.Date, 
        TUTOR_STUDENT.TopicCovered, TUTOR_STUDENT.BeginTime, TUTOR_STUDENT.EndTime, TUTOR_STUDENT.TotalMinutes, Tutors.TutorName, TUTOR_STUDENT.Categories
        FROM Instructors
        INNER JOIN Class
        ON Instructors.InstructorID = Class.InstructorID
        INNER JOIN CLASS_STUDENT
        ON Class.ClassID = CLASS_STUDENT.ClassID
        INNER JOIN Students
        ON CLASS_STUDENT.StudentUsername = Students.StudentUsername
        INNER JOIN TUTOR_STUDENT
        ON Students.StudentUsername = TUTOR_STUDENT.StudentUsername
        INNER JOIN Tutors
        ON TUTOR_STUDENT.TutorUsername = Tutors.TutorUsername
        WHERE TUTOR_STUDENT.Date >= '$StartDate' AND TUTOR_STUDENT.Date <= '$EndDate' AND TUTOR_STUDENT.ClassID = CLASS_STUDENT.ClassID
        ORDER BY TUTOR_STUDENT.Date";

    $result = mysqli_query($link, $sql);

    if ($result->num_rows > 0)
    {

        echo " 
                    <table>
                        <tr>
                            <th>Date</th>
                            <th>Class Name</th>
                            <th>Year</th>
                            <th>Semester</th>
                            <th>Topic Covered</th>
                            <th>Begin Time</th>
                            <th>End Time</th>
                            <th>Total Minutes</th>
                            <th>Tutor Name</th>
                            <th>Student Name</th>
                            <th>Catergory</th>

                        </tr>
                            ";

                            // output data of each row
                while($row = $result->fetch_assoc()) 
                {
                        echo "
                            <tr>
                                <td>".$row["Date"]. "</td>
                         ";
                         echo "
                                <td>".$row["ClassName"]. "</td>
                         ";
                         echo "
                                <td>".$row["Year"]. "</td>
                         ";
                         echo "
                                <td>".$row["Semester"]. "</td>
                         ";
                         echo "
                                <td>".$row["TopicCovered"]. "</td>
                         ";
                         echo "
                                <td>".$row["BeginTime"]. "</td>
                         ";
                          echo "
                                <td>".$row["EndTime"]. "</td>
                         ";
                         echo "
                                <td>".$row["TotalMinutes"]. "</td>
                         ";
                          echo "
                                <td>".$row["TutorName"]. "</td>
                         ";
                         echo "
                                <td>".$row["StudentName"]. "</td>
                         ";
                         echo "
                                <td>".$row["Categories"]. "</td>
                         ";

                        echo "</tr>";
                                   }
                        echo "</table>";

    }
    else
        echo "You have no tutoring sessions between those two dates.";

     if (isset($_POST['downloadAdminDate']))
    {

         $data = "Date,ClassName,Year,Semester,TopicCovered,BeginTime,EndDate,TotalMinutes,TutorName,StudentName,Categories\n";
         while($row = mysqli_fetch_array($result))
         {
            $data.=$row['Date'].",";
            $data.=$row['ClassName'].",";
            $data.=$row['Year'].",";
            $data.=$row['Semester'].",";
            $data.=$row['TopicCovered']."\n";
            $data.=$row['BeginTime'].",";
            $data.=$row['EndTime'].",";
            $data.=$row['TotalMinutes'].",";
            $data.=$row['TutorName'].",";
            $data.=$row['StudentName']."\n";
            $data.=$row['Categories']."\n";

         }

        $data_final = chr(255).chr(254).mb_convert_encoding($data, "UTF-16LE","UTF-8");
        header("Content-type: text/csv; charset=UTF-8");
        header("Content-Disposition: attachment; filename=Export.csv");

        print $data_final;
        exit;


    }



    mysqli_close($link);


?>

这是HTML:

    <p id ="byDate"> Search all tutoring sessions by date.</p>
        <form id ="SearchByDate" action ="AdminDate.php" method="post">
            Start Date <input = "textbox" type="date" name ="startDate" value="" required/>
            End Date <input = "textbox" type="date" name ="endDate" value="" required/> </br>
            </br>
            <button class ="button" type="submit" name = "searchDateSubmit" id ="searchDateSubmit" value ="submit"> Submit </button>
        </form>
        </br>
        </br>

        <div id ="searchDateContainer"> Date results are displayed here.
        </div>
        </br>
        </br>
         <form id ="downloadAdminDate" action="AdminDate.php" method="post">
        <button class ="download" type="submit" name ="downloadAdminDate" id              ="downloadDate" value="submit"> Download File </button>
        </form>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您似乎正在输出

 <table>

之前的

 header("Content-type: text/csv; charset=UTF-8"); 

里面的最后一个if。 您不能这样做,因为在输出任何内容之前需要输出标题。 我建议你把代码放在

if (isset($_POST['downloadAdminDate'])) {

在另一个名为AdminDateDownload.php的php文件中 然后将第二个表单的操作更改为:

<form id ="downloadAdminDate" action="AdminDateDownload.php" method="post" target="_blank">

添加目标=&#34; _blank&#34;所以你更容易理解发生的事情。