Save php form data to a text file

Discussion in 'PHP' started by jojo57, Mar 28, 2011.

  1. #1
    I copied this code from Captain Morgan. The problem I am having is that nothing ever gets written in the data.txt file. I am a newbie trying to learn php. Other than this file and my data.txt file, is their any other files I need to create?

    Code I am using:
    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['data'];
    $file = "data.txt";

    $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");

    fclose($fp);
    echo "Saved to $file successfully!";

    }
    ?>

    <form name="form1" method="post" action="index2.php?saving=1">
    <textarea name="data" cols="100" rows="10">
    Vehicle Make:
    Vehicle Model:
    ---------------------------------------------
    </textarea>
    <br>
    <input type="submit" value="Save">
    </form>
    <p>

    <a href="http://www.richu.5gbfree.com/data.txt"><b>VIEW<b></a>

    Your help is deeply appreciated.
     
    jojo57, Mar 28, 2011 IP
  2. theofficialSJ

    theofficialSJ Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Iam a zend certified php 5 programmer here. Let me know it in details
     
    theofficialSJ, Mar 28, 2011 IP
  3. chandan123

    chandan123 Prominent Member

    Messages:
    11,586
    Likes Received:
    578
    Best Answers:
    0
    Trophy Points:
    360
    #3
    try to use saving variable as hidden in form fields and see
     
    chandan123, Mar 28, 2011 IP
  4. jareso

    jareso Well-Known Member

    Messages:
    70
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Do you have your "data.txt" enabled as writeable on your server???
    Chmod it to 777 or set it manualy in your ftp client to writeable.
     
    jareso, Mar 28, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    or set your php to suphp to allow for writing as well
     
    srisen2, Mar 28, 2011 IP
  6. hunterman9

    hunterman9 Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    I'm guessing file permissions as well. Check your servers error logs to see if anything shows up that would be more helpful.
     
    hunterman9, Mar 28, 2011 IP
  7. jojo57

    jojo57 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you for your ideas. The problem was that I misspelled 'index2.php'. I forgot the '2'. Once this was corrected, the script worked.
     
    jojo57, Mar 31, 2011 IP