I wrote some code to save and download texts in the textarea as file, i pass textarea value to server side PHP code using Ajax. Here my Ajax code:
function saveFile()
{
document.getElementById("save").childNodes[0].nodeValue="Saving";
var values = {
"txTitle": document.getElementById("title").value,
"tex": document.getElementById("textar").value
};
$.ajax({
url: "SaveFile.php",
type: "POST",
data: values,
cache: false,
success: function(data){
document.getElementById("save").childNodes[0].nodeValue="Save";
}
});
return false;
}
And here the code in SaveFile.php file:
<?php
$tex = $_POST['tex'];
$txTitle = str_replace(array('\'', '"'), '', trim($_POST['txTitle']));
header('Content-Description: File Transfer');
header('Connection: Keep-Alive');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.str_replace(' ','-',$txTitle));
header('Content-Length: ' . mb_strlen($tex, '8bit'));
echo $tex;
?>