+ Reply to Thread
Results 1 to 9 of 9

Thread: Saving data from "form to a file"

  1. #1
    Newbie ankit is an unknown quantity at this point ankit's Avatar
    Join Date
    Feb 2010
    Location
    Indore, INDIA
    Age
    21
    Posts
    2

    Saving data from "form to a file"

    Hi
    I am a Fedora 10 user. I want to create a form that can accept form data and save it to a file. For that, I used the following code, but the file (names.txt) was not created. Can someone please tell me what to do??
    Code:
    <html>
    <head></head>
    <form method="POST">
    <pre>
    </br> First Name <input type="text" name="first"/>
    </br>  Last Name <input type="text" name="last"/>
    </br>       <input type="submit" value="Save the data"/>
    </pre>
    </form>
    <?php
    $fp 
    fopen("./names.txt",'a');
    if(
    $_POST['first'] and $_POST['last']){
    $name$_POST['first']." ".$_POST['last']."\n";
    print(
    $name);
    fputs(fp,$name);
    }
    fclose($fp);
    ?>
    </html>
    Last edited by Orjan; 02-08-2010 at 07:09 AM. Reason: Please use code tags!

  2. #2
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,613
    Blog Entries
    7

    Re: Saving data from "form to a file"

    the row

    Code:
    fputs(fp,$name); 
    shall be

    Code:
    fputs($fp,$name); 
    you simply missed the dollar sign for the variable $fp.

    I got an E_WARNING for that, didn't you?
    __________________________________________
    Ask me: Orjan | Contribute to the Wiki! | Make your own Programming blog!
    I usually play eRepublik and Travian when I'm not on CodeCall

  3. #3
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,066
    Blog Entries
    9

    Re: Saving data from "form to a file"

    Hello mate..
    I wrote a tutorial Create files with php
    Maybe this can help you..
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

  4. #4
    Newbie ankit is an unknown quantity at this point ankit's Avatar
    Join Date
    Feb 2010
    Location
    Indore, INDIA
    Age
    21
    Posts
    2

    Re: Saving data from "form to a file"

    I tried both the ways.

    First I corrected the mistake of $. But it did not work. So, I tried Jaan's method, but still no file was created.

    Actually, I am working over PHP on my desktop and not on any server. Is that the reason for the file not being created.
    Please help!

  5. #5
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,066
    Blog Entries
    9

    Re: Saving data from "form to a file"

    Code:
    $name $_REQUEST['name']." ".$_REQUEST['last'];
    $file "../names.txt";
    $open fopen($file"a");
    if(!
    fwrite($open$name){
    echo 
    "No names were written to file. Check permissions!";
    }else{
    fclose($open);

    well.. this should do the trick..
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

  6. #6
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,613
    Blog Entries
    7

    Re: Saving data from "form to a file"

    I tried the code with success on my windows under xampp...
    __________________________________________
    Ask me: Orjan | Contribute to the Wiki! | Make your own Programming blog!
    I usually play eRepublik and Travian when I'm not on CodeCall

  7. #7
    Learning Programmer webcodez is an unknown quantity at this point webcodez's Avatar
    Join Date
    Feb 2010
    Posts
    51

    Re: Saving data from "form to a file"

    Would rather use $_POST instead of $_REQUEST as $_REQUEST variables can as well be created through link ( contains both $_POST and $_GET variables ). However that's just my view on it.

  8. #8
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,613
    Blog Entries
    7

    Re: Saving data from "form to a file"

    Quote Originally Posted by webcodez View Post
    Would rather use $_POST instead of $_REQUEST as $_REQUEST variables can as well be created through link ( contains both $_POST and $_GET variables ). However that's just my view on it.

    Sometimes, you might wanna mix get and post, and sometimes, you want to make your code generic to work with both, as you might reuse it in many places, then $_REQUEST is great.
    __________________________________________
    Ask me: Orjan | Contribute to the Wiki! | Make your own Programming blog!
    I usually play eRepublik and Travian when I'm not on CodeCall

  9. #9
    Learning Programmer webcodez is an unknown quantity at this point webcodez's Avatar
    Join Date
    Feb 2010
    Posts
    51

    Re: Saving data from "form to a file"

    That's true.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 0
    Last Post: 11-19-2009, 12:20 AM
  2. Detect Character Set
    By dargueta in forum General Programming
    Replies: 14
    Last Post: 09-10-2009, 04:55 PM
  3. Really weird error compressing and saving data
    By ForeverNoobie in forum C and C++
    Replies: 6
    Last Post: 12-27-2008, 08:31 AM
  4. Choosing the right language / data structure / Interface
    By ups in forum General Programming
    Replies: 1
    Last Post: 09-20-2008, 09:11 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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