Reputation: 111
I have my head around Jquery now but want to save a text area to a txt file. With the ultimate goal as being able to upload a text file to the same place as well. What am I missing in my code?
Not sure where I am define the textarea in this code or whether it should be in another file or if the html file should have the suffix of php?
Cheers below is my attempt at the php.
CODE
<?php
if(isset($_POST['submit_save'])) {
$file = "output.txt";
$output = $_POST['output_str'];
file_put_contents($file, $output);
$text = file_get_contents($file);
header("Content-type: application/text");
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
}
else
{
$_POST['output_str'] = "";
}
?>
</head>
<body>
<input id="submit_save" type="button" value="save" />
</br></br></br>
<div id="opt"></div>
</body>
</html>
Upvotes: 1
Views: 506
Reputation: 147
I think you are trying something as this.
<?php
if(isset($_POST['textfield'])) {
$file = "output.txt";
$output = htmlspecialchars ($_POST['textfield'] );
file_put_contents($file, $output );
$text = file_get_contents($file);
header("Content-type: application/text");
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
exit;
}
?>
<html>
<head>
<title>Your app</title>
</head>
<body>
<form method="POST">
<textarea name="textfield"></textarea>
<input id="submit_save" type="button" value="save" />
</form>
</body>
</html>
Upvotes: 0
Reputation: 9480
Use the readfile function and don't forget to "exit" after to not corrupt the file. Like Enve said, POST array keys are html "name".
Here an example from the man pages :
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Source : http://php.net/manual/en/function.readfile.php
Upvotes: 0
Reputation: 37915
Your PHP script runs on your webserver, not in the browser. The result of the script is what the browser receives!
When reading your question(s), it is not clear (to me) whether you want to send the entered data (of the text area) to the browser as a file or as a HTML page. You seem to want both, which is not possible, choose one.
Fab Sa already demonstrated how to send a file to the browser from PHP, so I will not discuss this further.
To fill in the entered data in the text area in a HTML page, you must add a <textarea>
-tag in a HTML <form>
. Something like this:
<form method="post" action="<?= $PHP_SELF ?>">
<textarea name="output_str"><?= $_POST['output_str'] ?></textarea>
<input id="submit_save" name="submit_save" type="button" value="save" />
</form>
And remove this part of your code (that partially sends the data as a file):
$text = file_get_contents($file);
header("Content-type: application/text");
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
Note: that for safety reasons you should never use $_POST['output_str']
without sanitizing it (which I left out in the example to keep it clear).
Upvotes: 0
Reputation: 12020
$_POST
is for name. You need to add name to #submit_save
<input name="submit_save" id="submit_save" type="button" value="save" />
Upvotes: 2