help me in storing the data in file

Discussion in 'PHP' started by nawabsheriff, Jan 21, 2009.

  1. #1
    hi

    i am a beginner of php

    i just want to know the code or procedure to write a data in file automatically when submit button is clicked..

    for example.

    if i am getting a data from the user like name,age etc....after finishing the form ,if user clicks the submit button ,it should be automatically store in word pad or note pad...

    pls help me in this problem..
     
    nawabsheriff, Jan 21, 2009 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    search for the php functions
    fopen,fwrite
     
    bartolay13, Jan 22, 2009 IP
  3. fundu

    fundu Well-Known Member

    Messages:
    1,230
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    185
    Digital Goods:
    2
    #3
    Alternatively you can use mysql and save it to database.
     
    fundu, Jan 22, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
  5. bird.23

    bird.23 Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you can try this code:

    <?
    if(!$_POST['submit'])
    {
    ?>
    <form method="post" action="">
    <input type="text" name="name" value="">
    <input type="submit" name="submit">
    </form>
    <?
    }
    else
    {
    $name = $_POST['name'];
    $fp = fopen("new.txt",w);
    if($fp != null)
    {
    fwrite($fp,$name);
    }

    }
    ?>
     
    bird.23, Jan 22, 2009 IP
  6. t3nt3tion

    t3nt3tion Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Remember to use fclose($fp) too.
     
    t3nt3tion, Jan 22, 2009 IP
  7. bird.23

    bird.23 Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    sorry forgot to put,i was just telling the way to do...plz add fclose($fp) after fwrite($fp,$name)
     
    bird.23, Jan 22, 2009 IP
  8. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Here is the Correct Version of Code. It will create text file if file doesn't exist before.
    <?php
    if(!$_POST['submit'])
    {
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" name="name">
    <input type="submit" name="submit">
    </form>
    <?php
    }
    else
    {
    $name = $_POST['name'];
    $fp = fopen("myfile.txt","a+") or exit("unable to open the file");
    if($fp != null)
    {
    fwrite($fp,$name);
    }
    fclose($fp);


    }
    ?>
     
    mrmaf, Jan 24, 2009 IP