Am I doing something wrong with the PHP? Trying to save form to TXT

Discussion in 'PHP' started by innovativecs12, Dec 30, 2010.

  1. #1
    So I have been working on this for three days straight and just can't figure it out, maybe I just need some sleep but I finally got a working form and php code that saves contact form data to a txt file. The only problem is, when I try it, It only saves the message section, it does not save the Name and Email and Subject. If someone could help me out with this that would be awesome. I am relatively new to PHP sp bear with me.

    The html form I am using is:


    <html>
    <head>
    <title>Your Informations</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form action="test5.php?savedata=1" method="post">
    Your Name: <input type="text" name="name"><br>
    Your Email:<br> <input type="text" name="email"><br>
    Your Subject:<br> <input type="text" name="Subject"><br>
    Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>


    The PHP I am Using is:

    <?php

    $savedata = $_REQUEST['savedata'];
    if ($savedata == 2){
    $data = $_POST['name'];
    $data = $_POST['email'];
    $data = $_POST['subject'];
    $data = $_POST['message'];
    $file = "YOURDATAFILE.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 "Your Form has been Submitted!";

    }
    ?>
     
    innovativecs12, Dec 30, 2010 IP
  2. mikecampbell

    mikecampbell Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Every time you assign something to $data with the = operator you erase the value $data held previously. Use the .= operator to append instead of overwriting.

    $data = $_POST['name'];
    $data .= $_POST['email'];
    $data .= $_POST['subject'];
    $data .= $_POST['message'];
    PHP:
     
    mikecampbell, Dec 30, 2010 IP
  3. innovativecs12

    innovativecs12 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank You So much ! That worked perfectly! My only question now is how to I separate the fields when they save. For example when it saves it saves like this lo how are you. I would like it to save like this: name hello how are you.
     
    innovativecs12, Dec 30, 2010 IP
  4. mikecampbell

    mikecampbell Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $data = $_POST['name']." ".$_POST['email']." ".$_POST['subject']." ".$_POST['message'];
    PHP:
    or

    $data = $_POST['name']."\n".$_POST['email']."\n".$_POST['subject']."\n".$_POST['message'];
    PHP:
     
    mikecampbell, Dec 30, 2010 IP
  5. innovativecs12

    innovativecs12 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank you so much everyone for the help seriously the best help I have ever gotten on this stuff.
    Take care and happy new year!
     
    innovativecs12, Dec 30, 2010 IP