I'm currently trying to figure out how I would be able to export the table result done by the search.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=XeIP$
<script src="js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<title>AJAX Persons Example - Displaying FILES</title>
<script>
$(document).ready(function() {
$( "#AthleteName" ).change(function() {
$.ajax({
url: 'searchPlayer.php',
data: {searchName: $( "#AthleteName" ).val()},
success: function(data){
$('#Nameresult').html(data);
}
});
});
});
</script>
</head>
<body>
<h3>Olypmic Dataset</h3>
<input class="xlarge" id="AthleteName" type="search" size="30" placeholder="sid contains"/>
<div id="Nameresult">Search Result</div>
<br/><br/>
</body>
</html>
Above code is my index html which will display the results, and I'm using searchPlayer.php that does the function.
<?php
require "dbutil.php";
$db = DbUtil::loginConnection();
$stmt = $db->stmt_init();
if($stmt->prepare("select * from athlete where Name like ?") or die(mysqli_error($db))) {
$searchString = '%' . $_GET['searchName'] . '%';
$stmt->bind_param(s, $searchString);
$stmt->execute();
$stmt->bind_result($ID, $Name, $Sex, $Height, $Weight);
$col_att = array();
echo "<form method='post' action='download.php'><input type='submit' value='Export' name='Export'>";
echo "<table border=1><th>ID</th><th>Name</th><th>Sex</th><th>Height</th><th>Weight</th>\n";
while($stmt->fetch()) {
echo "<tr><td>$ID</td><td>$Name</td><td>$Sex</td><td>$Height</td><td>$Weight</td>";
echo '<td><a href="AthleteSite.php?ID='. $ID .'">View More</a></td></tr>';
$col_att[] = array($ID, $Name, $Sex, $Height, $Weight);
}
echo "</table>";
$serialize_user_arr = serialize($col_att);
echo "<textarea name='export_data' style='display: none;'><?php echo $serialize_user_arr; ?></textarea></form>"; $
$stmt->close();
}
$db->close();
?>
However, when the search result comes and I try to export, it just creates an empty csv file. I'm not sure what's going on that could create this problem. Is the mechanism that is done on searchPlayer.php incorrect?
<?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);
// file creation
$file = fopen($filename,"w");
foreach ($export_data as $line){
fputcsv($file,$line);
}
fclose($file);
// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");
readfile($filename);
// deleting file
unlink($filename);
exit();
This download.php serves as creating the csv file.