Export to Excel from html data using PHP

I have displayed a table of data from MYSQL DB. I have a button to export to excel. When I click on the button the excel file is created but it is empty or sometimes nothing is happening. What could be wrong here?

I have code like below:


                <div id="div_users">
                        <table class="table table-striped">
                            <thead class="thead-dark">
                                <th class="mn_th01">#</th>
                                <th class="mn_th02">Date</th>
                                <th class="mn_th03">Name</th>
                                <th class="mn_th04">Place</th>
                                <th class="mn_th05">Notes</th>
                                <th class="mn_th06">Sales Exec.</th>
                                <th class="mn_th07">Image</th>

                                    $date = date('d/m/Y', time());
                                    echo $date . "\n";                                                                      


                                    $query = "SELECT * FROM ua_myadd_details WHERE STR_TO_DATE(notes_date,'%d/%m/%y') = STR_TO_DATE('$date', '%d/%m/%y') ORDER BY M.id DESC";

                                    $result = mysqli_query($bd,$query);
                                    if(mysqli_num_rows($result) > 0)
                                        while($row = mysqli_fetch_assoc($result)) {

                                            <td><?php echo $count; ?></td>
                                            <td><?php echo $row["notes_date"]; ?></td>
                                            <td><?php echo $row["firstname"] . " " . $row["lastname"]; ?></td>
                                            <td><?php echo $row["city"]; ?></td>
                                            <td><?php echo $row["notes"]; ?></td>
                                            <td><?php echo $row["executive"]; ?></td>
                                        <?php $count++; } 

                                        <td colspan="7">
                                            <?php echo "No Data Found"; ?>
                                    <?php } ?>

I have a button like this:

<div class="col-md-3">

                           <button name="create_excel" id="create_excel" class="btn btn-success btn-block"><i class="fas fa-arrow-alt-circle-down"></i> Export to Excel</button>


In the script I have written:


        var excel_data = $('#div_users').html();  
        var page = "export.php?data=" + excel_data;  
        window.location = page;  



     header('Content-Type: application/vnd.ms-excel');  
     header('Content-disposition: attachment; filename='.rand().'.xls');  
     echo $_GET["data"];