Reputation: 9621
I have the following php script which checks the submitted value of the check-box elements and prints their values if those were checked.
<?php
echo '<table class="features-table">';
echo "<tbody>";
for ($i=1;$i<=2284;$i+=1) {
if($_POST[$i]) {
echo"<tr>";
echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i]. "</a></td>";
echo "</tr>";
}
}
?>
I want to make this data shown to the user, available for download as text file. How should I create the download button. Do I need separate php script in that case how will I get the form data submitted to this php script?
From google search I got that I need to use code similar to below
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=list.txt");
header("Pragma: no-cache");
header("Expires: 0");
echo 'data1,data2,data3...';
But I am not getting how do I integrate it with current php script.
Thanks
Upvotes: 1
Views: 2963
Reputation: 9621
Posting the code which may help others in future
After the answer by sudip I did the following changes
<?php
$list = "Unknown words from GSL\n\n";
echo '<table class="features-table">';
echo "<tbody>";
for ($i=1;$i<=2284;$i+=1) {
if($_POST[$i]) {
echo"<tr>";
echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i]. "</a></td>";
$list = $list . $_POST[$i] . "\n";
echo "</tr>";
}
}
echo'
<form action="download.php" method="post">
<input type="hidden" name="wordlist" value="'. $list . '">
<center><input type="submit" name="submit_parse" style="margin-bottom: 20px; margin-top: 10px;width:200px; font-face: "Comic Sans MS"; font-size: larger; " value=" Download as a text file "> </center>
</form>
';
?>
I had to create separate php file since header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
Contents of download.php
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=list.txt");
header("Pragma: no-cache");
header("Expires: 0");
echo $_POST["wordlist"] ;
?>
Upvotes: 1
Reputation: 2850
I dont see any necessity to create a file on the fly.
I would do it in the following way....
Make the data available as hidden fields and enclose the fields in a form tag along with the download button. When a user clicks the download button, all the form data will be available in the script. Then you can do whatever action you want to do with the data.
Upvotes: 1