Closed Thread
Results 1 to 9 of 9

Thread: Saving data from "form to a file"

  1. #1
    ankit's Avatar
    ankit is offline Newbie
    Join Date
    Feb 2010
    Location
    Indore, INDIA
    Posts
    2
    Rep Power
    0

    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 05:09 AM. Reason: Please use code tags!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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?
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  4. #3
    Jaan Guest

    Re: Saving data from "form to a file"

    Hello mate..
    I wrote a tutorial http://forum.codecall.net/php-tutori...files-php.html
    Maybe this can help you..

  5. #4
    ankit's Avatar
    ankit is offline Newbie
    Join Date
    Feb 2010
    Location
    Indore, INDIA
    Posts
    2
    Rep Power
    0

    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!

  6. #5
    Jaan Guest

    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..

  7. #6
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    Re: Saving data from "form to a file"

    I tried the code with success on my windows under xampp...
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  8. #7
    webcodez's Avatar
    webcodez is offline Programmer
    Join Date
    Feb 2010
    Posts
    148
    Rep Power
    0

    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.

  9. #8
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  10. #9
    webcodez's Avatar
    webcodez is offline Programmer
    Join Date
    Feb 2010
    Posts
    148
    Rep Power
    0

    Re: Saving data from "form to a file"

    That's true.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Alex G. CSS "Sexy Buttons" - what about "submit on 'ENTER'" ?
    By shackrock in forum JavaScript and CSS
    Replies: 7
    Last Post: 12-05-2010, 04:57 PM
  2. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  3. Export access table into csv file delimited ";"
    By sal21 in forum Visual Basic Programming
    Replies: 2
    Last Post: 06-16-2010, 07:07 AM
  4. form.style.display = "none" ???
    By dimitry in forum JavaScript and CSS
    Replies: 9
    Last Post: 06-04-2009, 11:13 PM
  5. Storing "unseen" data of a forum
    By Orjan in forum PHP Development
    Replies: 22
    Last Post: 09-26-2008, 01:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

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