...

View Full Version : Save text from a textbox to a file



Killfrenzy
04-15-2012, 12:14 PM
Hello everyone,

I was wondering how to save text from a textbox as seen below to a file? (I have a website using HTML, PHP and Javascript)

http://i44.tinypic.com/2v16crm.png

What I want: When users click Save Notes a popup appears and users can choose the location to save the text file. The text does not have to be saved to the server.




function showNotesBox() {
$this->tag = "<div id='notes'>";
$this->tag .= "<h2 id='title'>Notities</h2>";
$this->tag .= "<form action='' method='post'>";
$this->tag .= "<textarea rows='20' cols='50'></textarea><br />";
if (isset($SESSION['status'])) {
$this->tag .= "<a href='index.php?page=save'><input class='button' type='button' value='Save note ' /></a>";
$this->tag .= "<a href='index.php?page=share'><input class='button' type='button' value='Share with' /></a>";
$this->tag .= "<a href='index.php?page=help'><input class='button' type='button' value='Help the community!' /></a>";
} else {
$this->tag .= "<a href='index.php?page=save'><input class='button' type='button' disabled='disabled' value='Save note' /></a>";
$this->tag .= "<a href='index.php?page=share'><input class='button' type='button' disabled='disabled' value='Share with' /></a>";
$this->tag .= "<a href='index.php?page=help'><input class='button' type='button' disabled='disabled' value='Help the community!' /></a>";
}
$this->tag .= "</form></div>";
echo $this->tag;
}

Anyone knows how to accomplish this? :)

mlseim
04-15-2012, 05:34 PM
Slightly different than your example, but it might give you ideas ...



<?php
session_start();

if (isset($_POST['save_note'])) {
save_file(mb_convert_encoding($_POST['text'],'utf-16','utf-8'));
exit;
}
else{
showNotesBox();
}

function save_file($txt) {
$filename = "note_" . date("Y-M-d", time());
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=" . $filename . ".txt");
ob_start();
print $txt;
}

function showNotesBox() {
$string = "<div id='notes'>";
$string .= "<h2 id='title'>Notities</h2>";
$string .= "<form action='' method='post'>";
$string .= "<textarea rows='20' cols='50' name='text'></textarea><br />";
if (isset($SESSION['status'])) {
$string .= "<a href='savenote.php?page=save'><input class='button' type='button' value='Save note ' /></a>";
$string .= "<a href='savenote.php?page=share'><input class='button' type='button' value='Share with' /></a>";
$string .= "<a href='savenote.php?page=help'><input class='button' type='button' value='Help the community!' /></a>";
} else {
$string .= "<input class='button' type='submit' name='save_note' value='Save note' />";
$string .= "<a href='savenote.php?page=share'><input class='button' type='button' disabled='disabled' value='Share with' /></a>";
$string .= "<a href='savenote.php?page=help'><input class='button' type='button' disabled='disabled' value='Help the community!' /></a>";
}
$string .= "</form></div>";
echo $string;
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum