So I was trying to export mysql table to csv file via php. This is the code
<?php if (isset($_POST['exp'])) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('User ID', 'User Name', 'Password'));
$con = mysqli_connect('localhost', 'root', 'pass', 'xyz');
$rows = mysqli_query($con, 'SELECT * FROM users');
while ($row = mysqli_fetch_assoc($rows)) {
fputcsv($output, $row);
}
fclose($output);
mysqli_close($con);
}
?>
<div>
<form action="#" method="post">
<input type="submit" value="Export" name="exp" />
</form>
</div>
Every thing is fine but the html part also gets dumped in the csv file. here is screen shot of the csv

Why is that happening? Am I doing anything wrong?
HTMLandPHP.