Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Save text from a textbox to a file

    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)



    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.

    Code:
     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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    Slightly different than your example, but it might give you ideas ...

    PHP Code:
    <?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;
        }    
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •