Results 1 to 8 of 8

Thread: PHP Nub

  1. #1

    Thread Starter
    Fanatic Member duc's Avatar
    Join Date
    Oct 2002
    Location
    STEAM
    Posts
    702

    PHP Nub

    Yes, im a newb to php. What I need is pretty simple...i think.

    i have this page here

    It doesn't really do anything right now. What I was wondering is how can I have whatever they type in the box, be sent to a text file on my server when they click send.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    you can do something like:

    PHP Code:
    <?
      //check if the form has been submitted
      if(!empty($_POST['textfield'])){
        /************************
         * this WILL replace anything in the file "file.txt", if you want
         * it to just add on to the file, replace "w+" with just "w"
         ************************/
        //open the file for writing purposes
        $open = fopen("file.txt", "w+");
        //write to the file
        fwrite($open, $_POST['textfield']);
        fclose($open);
        echo "successfully written to!\n";
        echo "<br><br>\n";
        echo "<pre>\n$_POST[textfield]\n</pre>\n";
      }else{
    ?>
    <form method="post" action="fwrite.php">
      <textarea name="textfield" cols="77" rows="16">insert duelie script here</textarea><br><br>
      <input type="submit" name="Submit" value="send to muffinator">
    </form>
    <?
      }
    ?>
    I changed your form because it wouldn't work how you originally set it up.. your <textarea> wasn't in a form, and so it wasn't being properly submitted.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Fanatic Member duc's Avatar
    Join Date
    Oct 2002
    Location
    STEAM
    Posts
    702
    Hey that worked great thanks alot man. Is there a way I could have it sent to me in an email? Oh and one other thing. I tried changing the w+ to just w but it still erases the previous contents of the txt file instead of adding on to it.

    -duc

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    ooh, I mixed some of it up.. "w" is for writing only; "w+" is for reading and writing, so use "a" instead, which will append to the current file, and "a+" if you also want to read it. The script I gave you doesn't read anything, so you just need to use "a".

    I'm not used to using fopen() anymore..

    Also, for sending yourself an email, check out PHP's built-in mail() function.. I don't have any experience with it, so I can't really help you out there.. Link's here: http://ca2.php.net/manual/en/ref.mail.php
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Fanatic Member duc's Avatar
    Join Date
    Oct 2002
    Location
    STEAM
    Posts
    702
    Thanks that worked great. If I wanted to make a simple little news system on my website, would it be safe to use the PHP writing to a text file like I just did? I would just post what I want in the text file, and have the textfile displayed in the news section. I know its not top notch secure but do you think its safe enough as long as know one knows the link to the actual fwrite.php [which i will change]

    -duc

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    It'd be better to work with a database for the news system, but yeah, you could work with text files.

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Like mendhak said, yes you can. Even though databases are more efficient, text files work, and they are a simple way of working with news.

    You can use the exact script you are using right now for it, or you can slightly modify it so that you are required to enter in a password, and even a username, if you want.

    I took my original script and tweaked it a bit to get this effect for you..

    PHP Code:
    <? 
      $password = "password";
      //check if the form has been submitted
      if(!empty($_POST['textfield'], $_POST['pass']) && $_POST['pass'] == $password){ 
        /************************ 
         * this WILL replace anything in the file "file.txt", if you want 
         * it to just add on to the file, replace "w+" with just "w" 
         ************************/ 
        //open the file for writing purposes 
        $open = fopen("file.txt", "w+"); 
        //write to the file 
        fwrite($open, $_POST['textfield']); 
        fclose($open); 
        echo "successfully written to!\n"; 
        echo "<br><br>\n"; 
        echo "<pre>\n$_POST[textfield]\n</pre>\n"; 
      }else{ 
    ?> 
    <form method="post" action="fwrite.php"> 
      password: <input type="text" name="pass" maxlength="15"><br><br>
      <textarea name="textfield" cols="77" rows="16">insert duelie script here</textarea><br><br> 
      <input type="submit" name="Submit" value="send to muffinator"> 
    </form> 
    <? 
      } 
    ?>
    That should work.. you just have to change the value of $password.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8

    Thread Starter
    Fanatic Member duc's Avatar
    Join Date
    Oct 2002
    Location
    STEAM
    Posts
    702
    thanks alot man. just what i wanted.

Posting Permissions

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



Click Here to Expand Forum to Full Width