SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to make a simple mini forum - need help

    hello,

    I am trying to make a simple mini forum where the posts that people send are on a text file (my mini database).

    I don't want to use MySQL to do this. I basically don't know how to get the page to remember the posts people submit in.

    I want it to read the txt file and then find all the posts and display them.

    It submits the posts to itself btw i.e.
    <form action="jsforum.php" method="post">

    Heres the code to the page:

    Code:
    <?php
    $fname = "jsforum.txt"; 
    $data = "New reply: \r\n"."Sending Date: ".date("F jS Y, h:iA.")."\r\n".$reply."\r\n \r\n"; 
    
    $fp = fopen($fname, "a+"); 
    if( !fwrite($fp, $data) ){ 
    echo "Problem writing data"; 
    }
    fclose($fp);
    print $reply."\r\n";
    ?>
    <html>
    <head>
    <title>Javascript Forum</title>
    </head>
    <body>
    <h1>Javascipt Forum</h1>
    <form action="jsforum.php" method="post">
    <textarea name="reply" rows="7" cols="50">
    </textarea>
    <p><p/>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    Hope someone can help with this one. I'm stumped.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  2. #2
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, i am quite new to php so please don't use too complicated language.

    Please stay simple.

    Thanks
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  3. #3
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why do you use file to save posts ??

    why not saving it in mysql dayabas ?

  4. #4
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because I tried setting up a MySQL database and it got really confusing so I decided to use a text file instead.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  5. #5
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Stephen,

    I played around with your code a bit, and came up with this.
    Maybe this can be a starting point for you:

    Code:
    <?php
    $fname = "jsforum.txt"; 
    $data = "<font face='verdana' color='gray' size='1'>New reply: <br />"."Sending Date: ".date("F jS Y, h:iA.")."</font><br />".$reply."<br /><br />"; 
    
    if($_POST[reply]){
      $fp = fopen($fname, "a+"); 
       if( !fwrite($fp, $data) ){ 
         echo "Problem writing data"; 
       }
      fclose($fp);
    }
    
      $f_in  = fopen('jsforum.txt', 'r');
      $file  = fread($f_in, filesize ('jsforum.txt'));
    
    ?>
    
    <html>
    <head>
    <title>Javascript Forum</title>
    </head>
    <body>
    <table border="1" cellpadding="6" width="500">
     <tr>
       <td>
    <?
      print stripslashes($file)."\r\n<br />";
      fclose($f_in);
    ?>
      </td>
     </tr>
    </table>
    
    <h1>Javascipt Forum</h1>
    <form action="jsforum.php" method="post">
    <textarea name="reply" rows="7" cols="50">
    </textarea>
    <p><p/>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    Don't forget to chmod the jsforum.txt file to 777

    Patrick

  6. #6
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. Unfortunately I can't get it to quite work. I'm not sure if I have done the chmod bit right. But even without it I get errors.

    Heres the very slightly changed code:

    Code:
    <?php
    $fname = "jsforum.txt";
    chmod("jsforum.txt",777);
    $data = "<font face='verdana' color='gray' size='1'>New reply: <br />"."Sending Date: ".date("F jS Y, h:iA.")."</font><br />".$reply."<br /><br />"; 
    
    if($_POST[reply]){
      $fp = fopen($fname, "a+"); 
       if( !fwrite($fp, $data) ){ 
         echo "Problem writing data"; 
       }
      fclose($fp);
    }
    
      $f_in  = fopen('jsforum.txt', 'r');
      $file  = fread($f_in, filesize ('jsforum.txt'));
    
    ?>
    
    <html>
    <head>
    <title>Javascript Forum</title>
    </head>
    <body>
    <table border="1" cellpadding="6" width="500">
     <tr>
       <td>
    <?
      print stripslashes($file)."\r\n<br />";
      fclose($f_in);
    ?>
      </td>
     </tr>
    </table>
    
    <h1>Javascipt Forum</h1>
    <form action="jsforum.php" method="post">
    <textarea name="reply" rows="7" cols="50">
    </textarea>
    <p><p/>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    And the errors I get:

    Warning: Use of undefined constant reply - assumed 'reply' in c:\program files\apache group\apache\htdocs\testforum.php on line 6

    Warning: Undefined variable: _POST in c:\program files\apache group\apache\htdocs\testforum.php on line 6

    Thanks for your help.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  7. #7
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw i have sorted out the first error. The "reply" needed quotes around it.

    But I'm still stuck on the second one.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  8. #8
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm quite interesting , is it possible to get data back from a text file ?

  9. #9
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    T-S-R ||TiTanIUM, are you good at php or are you like me, a beginner?
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  10. #10
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the second error, what version of php are you running? If it is prior to version 4.1, change $_POST to $HTTP_POST_VARS.

  11. #11
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I am running 4.0 at the moment. I've tried the code but I still get errors but 3 this time:

    Warning: fopen("jsforum.txt","a+") - Permission denied in c:\program files\apache group\apache\htdocs\testforum.php on line 7

    Warning: Supplied argument is not a valid File-Handle resource in c:\program files\apache group\apache\htdocs\testforum.php on line 8
    Problem writing data
    Warning: Supplied argument is not a valid File-Handle resource in c:\program files\apache group\apache\htdocs\testforum.php on line 11

    Do you know what's wrong now? I don't have a clue!

    Thanks for your help anyway.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  12. #12
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres the code again, just in case I changed something since the last one.

    Code:
    <?php
    $fname = "jsforum.txt";
    chmod("jsforum.txt",777);
    $data = "<tr><td><font face='verdana' color='gray' size='1'>New reply: <p></p>"."Sending Date: ".date("F jS Y, h:iA.")."</font><p></p>".$reply."<br /><br /></td></tr>"; 
    
    if($HTTP_POST_VARS['reply']){
      $fp = fopen($fname, "a+"); 
       if( !fwrite($fp, $data) ){ 
         echo "Problem writing data"; 
       }
      fclose($fp);
    }
    
      $f_in  = fopen('jsforum.txt', 'r');
      $file  = fread($f_in, filesize ('jsforum.txt'));
    
    ?>
    
    <html>
    <head>
    <title>Javascript Forum</title>
    </head>
    <body>
    <table border="1" cellpadding="6" width="500">
     <tr>
       <td>
    <?
      print stripslashes($file)."\r\n<br />";
      fclose($f_in);
    ?>
      </td>
     </tr>
    </table>
    
    <h1>Javascipt Forum</h1>
    <form action="testforum.php" method="post">
    <textarea name="reply" rows="7" cols="50">
    </textarea>
    <p><p/>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    Thanks
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  13. #13
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those errors indicate that the chmod() you are doing isn't actually working and changing the permissions to 777 on that file. Try chmod'ing the file manually outside of the script and trying it again.

  14. #14
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try chmod'ing the file manually outside of the script and trying it again.
    What do you mean by that? I don't know what you mean by manually and outside the script exactly.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  15. #15
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I just found this on chmod at php.net. The part that is of interest is that you have to add a zero to the beginning of your chmod option to put it in octal format. So, if you are chmoding for 600, it would have to be chmod("thefile", 0600).
    Last edited by krayziepjf; Apr 22, 2005 at 12:09. Reason: typo

  16. #16
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I understand that. I'll try that.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  17. #17
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't make a difference to the errors.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  18. #18
    SitePoint Zealot Kaystarmaker's Avatar
    Join Date
    Jan 2005
    Location
    The Netherlands
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try chmod("thefile", 0777)

  19. #19
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats what I tried. Sorry.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer


Bookmarks

Posting Permissions

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