Friday, April 12, 2013

PHP: add a download as csv file button in report page

Suppose  we have  a form to post data from MySQL to report.php
<form name="form1" action="report.php" method="POST">
Now we not only want to display data in the table in report.php, we also need ot add a button to download as CSV file.
In report.php below, we have a table to display data. We also  have a hidden input to transfer data
to new action script report_csvA.php to download as csv file.
     <form name="getcvs" action="report_csvA.php" method="POST">
         <input type="submit" name="submit" value="Download CSV file" class="input-button" />
   <table   cellspacing="0" border="1" style="font-size:10" width="100%">
      <!-- <th scope="col"><a href="#program_name" onclick="setSort('program_name')">Member Name</a></th> -->
      <!-- <th scope="col"><a href="#last_name" onclick="setSort('contact_last_name')">Society</a></th> -->
      <th scope="col" width=35% style="text-align:left" >Type of Activity </th>
      <th scope="col"  style="text-align:left" >Number of Activity</th>
      <th scope="col" style="text-align:left">Hours of Instruction</th>

        $count_basic_skills = $_POST['count1'];
        $count_hours_bs = $_POST['count2'];
         echo '<td>Basic Skills</td>';
         echo '<td>'.$count_basic_skills.'</td>';
         echo '<td>'.$count_hours_bs.'</td>';     

      <input type="hidden" name="count_basic_skills" value="<?php echo $count_basic_skills;?>">

      <input type="hidden" name="count_hours_bs" value="<?php echo $count_hours_bs;?>">

In  report_csvA.php, a csv file is produced for download:
header('Content-Type: application/x-excel');
header('Content-Disposition: attachment; filename="cesei_activity_log.csv"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$headers=array('Type of Activity','Number of Activity','Hours of Instruction');
 $row_1=array('Basic Skills', $_POST['count_basic_skills'],$_POST['count_hours_bs']);


1 comment:

  1. Hi there, in your report.php codes, you wanted to retrieve data and display on that page right? Which part of the code is to retrieve data from the table? And also, what does the count1 & count2 do ?