0

I am trying to search some results and download them into an excel sheet. downloading is happening. But not the searched data.all data get downloaded. Searching is happening perfectly. I want to download only searched data.

I think the problem is with the javascript. It doesn't take the parameter. If anyone can give an idea that would be a great help. I am using codeigniter.

Thanks in advace.

Model

function search_bookings($time) {

        $this->db->select('reservations.*');
        $this->db->from('reservations');
        $this->db->where('is_deleted', '0');
        $this->db->where('reservations.type like "'.$time.'%" or reservations.title like "'.$time.'%" and reservations.is_deleted like 0');               
        $query = $this->db->get();
        return $query->result();
    }

Controller

function search_reservations() {
        $reservation_service    = new Reservation_service();
        $time                   = $this->input->post('type', TRUE);
        $searched_results       = $reservation_service->search_bookings($time);
        $data['search_results'] = $searched_results;

        $this->load->view('dashboard/search_results', $data);
    }

function get_report() {
        $reservation_service = new Reservation_service();
        $time                   = $this->input->post('type', TRUE);
        $searched_results       = $reservation_service->search_bookings($time);
        $data['search_results'] = $searched_results;

        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=Report.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo $this->load->view('dashboard/excel_view', $data, TRUE);
    }

View

<div class="form-group"><br>
    <select name="selected_time" id="type" data-live-search="true">
        <option value="">Time or Hall</option>
        <option value="AM">AM</option>
        <option value="PM">PM</option>
        <option value="Royal Princess (Downstairs)">Royal Princess Ballroom (Downstairs)</option>
        <option value="Grand Kings (Upstairs)">Grand King's Ballroom (Upstairs)</option>
    </select>
    <button class="btn btn-primary" onclick="search()">Search</button>
    <button class="btn btn-info btn-xs" id="print_excel" onclick="generate_reports()">Generate Report</button>
</div>

<script type="text/javascript">
    function search() {
        var type = $('#type').val();

        $.ajax({
            type: "POST",
            url: '<?php echo site_url(); ?>/dashboard/search_reservations',
            data: "type=" + type,
            success: function(msg) {
                $('#search_res').html(msg);

            }
        });
    }

    //generate excel sheets
    $(document).on('click', '#print_excel', function() {
        var type = $('#type').val();        
        var win = window.open('<?php echo site_url(); ?>/dashboard/get_report?type=' + type);        
        win.focus();        
    });   

</script>

<div id="search_res">

</div>

excel_view

<?PHP
header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=Report.xls"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
//echo "Some Text";
?>
<html>
    <head>
        <style type="text/css">
            body{
                font-family:Arial, Helvetica, sans-serif;
                font-family:Arial, Helvetica, sans-serif;
                font-size:14px;
            }
            table.da-table
            {
                width:100%;
                margin:0;
                clear:both;
                font-family:Arial, Helvetica, sans-serif;
                font-size:12px;
                border-top:1px solid #cacaca;
                border-left:1px solid #cacaca;
                border-bottom:1px solid #cacaca;
                border-right:1px solid #cacaca;

                -moz-box-shadow:inset 1px 0 0 0 #f8f8f8;
                -webkit-box-shadow:inset 1px 0 0 0 #f8f8f8;
                -khtml-box-shadow:inset 1px 0 0 0 #f8f8f8;
                box-shadow:inset 1px 0 0 0 #f8f8f8;
            }
            .bottom-line{
                clear:both;
                display:block;
                border-bottom:1px solid #f1f1f1;
                height:1px;
                margin:20px 0;
                width:100%;
            }
        </style>
    </head>
    <body style="background-color:#FFF">
        <table width="100%" border="0" class="">

            <tr>
<!--                <td><img src="<?php echo base_url(); ?>uploads/article_logo/article_logo1431984948-rudy 3.png" /></td>-->

                <td ><p style="font-size:16px;font-weight:bold;">Detailed Report</p>                    
                </td>

            </tr>
        </table>
    <div id="search_res_excel">
        <div class="adv-table">                    
            <table  class="da-table" id="bookings_table" border="1">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Date</th>
                        <th>Hall</th>
                        <th>Time</th>
                        <th>Name</th> 
                        <th>Fax</th> 
                        <th>Telephone</th> 
                        <th>Address</th> 
                        <th>Paid Amount</th> 
                        <th>Menu No</th> 
                        <th>Price per plate</th> 
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    foreach ($search_results as $result) {
                        ?>
                        <tr id="bookings_<?php echo $result->id; ?>">
                            <td><?php echo ++$i; ?></td>
                            <td><?php echo $result->date_cal; ?></td>
                            <td><?php if ("Royal Princess (Downstairs)" == $result->title){ ?><?php echo "Royal Princess Ballroom (Downstairs)";}?>
                                <?php if ("Grand Kings (Upstairs)" == $result->title){ ?><?php echo "Grand Kings Ballroom (Upstairs)";}?>
                            </td>
                            <td><?php echo $result->type; ?></td>
                            <td><?php echo $result->description; ?></td>
                            <td><?php echo $result->fax; ?></td>
                            <td><?php echo $result->telephone_number; ?></td>
                            <td><?php echo $result->address; ?></td>
                            <td><?php echo $result->paid_amount; ?></td>
                            <td><?php echo $result->menu_no; ?></td>
                            <td><?php echo $result->menu_price_per_plate; ?></td>
                        <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
        <div class="bottom-line"></div>
        <table width="100%" border="0" class="">
            <tr>
                <td align="left" scope="col">Printed By : <?PHP echo $this->session->userdata('USER_NAME') ?></td>            
            </tr>
            <tr>
            <td width="33%" align="right" scope="col"><?PHP echo date("F j, Y, g:i a"); ?></td>
            </tr>
        </table>
    </body>
</html>
Rikesh
  • 26,156
  • 14
  • 79
  • 87
Ishani Pathinayake
  • 177
  • 1
  • 2
  • 15

1 Answers1

0

You need to post the type value. You are not getting the value because you are using get request

so your ajax segment will look somewhat like this

$(document).on('click', '#print_excel', function() {
var type = $('#type').val();    
$.ajax({
        type: "POST",
        url: '<?php echo site_url(); ?>/dashboard/get_report',
        data: {"type":type},
        success: function(msg) {
        }
    });
});

Or you can refer a considerably similer question in the given link and adopt the accepted answer there Replace window.open with post

Community
  • 1
  • 1
Harigovind R
  • 816
  • 8
  • 17