How to save page content to a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nma
    New Member
    • Aug 2007
    • 93

    How to save page content to a text file

    Hi

    I have a popup windows that display the database content and I want to have save button that save content to a text file.

    Most code I found is saving a text area.

    How do I do that?

    Thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, NMA.

    Create a new PHP script to load and save the database content and link to that file.

    You can tell the browser to display the content as text by using this line:
    [code=php]
    header('Content-type: text/plain; charset=charact erSetGoesHere') ;
    [/code]

    The value of charset is generally 'utf-8', but you might have a long string that begins with 'iso' if you develop on a Windows system.

    You can also create a force download script.

    Comment

    • nma
      New Member
      • Aug 2007
      • 93

      #3
      Hi,

      Sorry I'm a bit blurr on the complete code sinde my deadline is only one day left. Please can someone let me know the complete code.

      Should I make a hyperlink text(to activate the save link etc/save button) that will point to another php file say save.php that will contain all the headers information of php?

      I attached the code that display the database content

      [PHP]
      <?php
      session_start() ;


      if(!isset($_SES SION['user'])) {
      header('Locatio n: /Login.html');
      die();
      }
      ?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <SCRIPT LANGUAGE="JavaS cript">


      function varitext(text){
      text=document
      print(text)
      }

      </script>




      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>See All My Notes</title>
      <link rel="stylesheet " href="moviebrow ser2.css" type="text/css" title="Default" media="screen"/>
      <style type="text/css">
      .style2 {color: #FFFFCC}

      </style>
      </head>

      <span class="style2"> </span>
      <body bgcolor="#FFFFF F">

      <table width="750" border="0">

      <?php
      include_once('. ./ez_sql.php');
      ?>

      <tr>
      <td width="100" align="left" valign="top" class="movieinf o_headtext">Mov ie Title </td>
      <td width="300" align="left" valign="top" class="movieinf o_headtext">Not es </td>
      <td width="100" align="left" valign="top" class="movieinf o_headtext">Dat e </td>
      <td width="150" align="left" valign="top" class="movieinf o_headtext">Eve nt Category </td>
      <td width="100" align="center" valign="top" class="movieinf o_headtext">Eve nt No. <br/></td>
      </tr>
      <?php

      $notesDetails = $db->get_results("e xec spShow_Movie_An d_Events_Notes_ By_User " . $_SESSION['user']->userID ." ");


      foreach ($notesDetails as $notes)
      {
      ?>
      <tr>
      <td width="100" align="left" valign="top" class="displayN otesBox1"><?php echo $notes->movieTitle;? ></td>
      <td width="300" align="left" class="displayN otesBox1" valign="top"><? php echo $notes->noteText;?></td>
      <td width="100" align="left" valign="top" class="displayN otesBox1"><?php echo $notes->noteDate;?></td>
      <td width="150" align="left" valign="top" class="displayN otesBox1"><?php echo $notes->EventCategory; ?></td>
      <td width="100" align="center" valign="top" class="displayN otesBox1"><?php echo $notes->eventNo;?><b r/></td>
      </tr>

      <?php
      }
      ?>
      <tr>
      <td></br></br></br></br></td>
      </tr>

      <tr>
      <td>

      <DIV ALIGN="CENTER">
      <FORM>
      <INPUT NAME="print" TYPE="button" VALUE="Print this Page"
      ONCLICK="varite xt()">
      </FORM>
      </DIV>
      <!-- <form method="post">
      <input name="save" type="submit" value="Save My Notes"></td>
      </form> -->
      </td>
      </tr>
      </table>
      </body>
      </html>



      [/PHP]

      Comment

      • nma
        New Member
        • Aug 2007
        • 93

        #4
        Hi
        How do I change the code below if I want the filename will depend on username and not hardcode like $myFile = "Notes.txt" ; below? The filename will be say "username.t xt"

        Thnask

        [PHP]
        <?php
        session_start() ;

        // Make sure users are logged in.
        if(!isset($_SES SION['user'])) {
        header('Locatio n: /MB2Login.html') ;
        die();
        }
        ?>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Save Notes</title>


        </head>
        <html>

        <?php
        include_once('. ./ez_sql.php');
        ?>

        <body>
        <?php
        //to create a file
        $myFile = "Notes.txt" ;

        $fh = fopen($myFile, 'w') or die("can't open file");[/PHP]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, NMA.

          Try this:
          [code=php]
          $myFile = "{$_SESSION['user']}.txt";
          [/code]

          Comment

          • nma
            New Member
            • Aug 2007
            • 93

            #6
            thank you very much!!!!

            Comment

            Working...