Php Form to .txt

Discussion in 'PHP' started by Vvhy, Jan 1, 2009.

  1. #1
    Hello everyone I am very new to php. I am self taught and if possible could you please take me through this slow.
    I'm basically trying to make a form in PHP that when I submit will save the information in a text file.

    
    <form id="login_form" action=post.php method="post" autocomplete="off">
    <div class="section_form">
    <span style="float: left">Name:</span>
    
    <input style="float: right" size="20" type="text" name="name" maxlength="12">
    
    <br class="clear">
    </div>
    <div class="section_form">
    
    <span style="float: left">Password:</span>
    <input style="float: right" size="20" type="password" name="password" maxlength="20">
    <br class="clear">
    </div>
    <div class="section_form">
    <input type="submit" value="submit">
    </div>
    
    PHP:
    So what I was wondering was how do I get the persons name and password to save into a text file? Do I do something in post.php? Help me confused :confused:
     
    Vvhy, Jan 1, 2009 IP
  2. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    in your post.php

    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = $_POST["name"].",".$_POST["password"]."\n";
    fwrite($fh, $stringData);
    fclose($fh);
     
    fairuz.ismail, Jan 1, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    You can do it with 1 line.

    post.php:
    
    file_put_contents('file.txt', implode('|', $_POST) . PHP_EOL, FILE_APPEND);
    
    PHP:
     
    Kaizoku, Jan 1, 2009 IP
  4. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yea, i forgot about that =)

    but i think it's better to treat first the $_POST values since, if i'm not mistaken, using your code it will store the submit button value too.
     
    fairuz.ismail, Jan 2, 2009 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    nope, submit wasn't given a name attribute.
     
    Kaizoku, Jan 2, 2009 IP