Professional Web Applications Themes

Newbe php help - PHP Development

I have an HTML form with Firstname, lastname, address I want to save the firstname, lastname and address appended to a file in say results.txt, in the below format 1 entry per line... MyFirstname,MyLastname,MyAddress I know this is probebly easy, can someone help me with a code snippit that does this? thanks....

  1. #1

    Default Newbe php help

    I have an HTML form with Firstname, lastname, address

    I want to save the firstname, lastname and address appended to a file in say
    results.txt, in the below format 1 entry per line...

    MyFirstname,MyLastname,MyAddress

    I know this is probebly easy, can someone help me with a code snippit that
    does this?



    thanks.



    eboogyman Guest

  2. #2

    Default Re: Newbe php help

    I noticed that Message-ID: <vnlg8c4cao4940news.supernews.com> from
    eboogyman contained the following:
    >I want to save the firstname, lastname and address appended to a file in say
    >results.txt, in the below format 1 entry per line...
    >
    >MyFirstname,MyLastname,MyAddress
    >
    >I know this is probebly easy, can someone help me with a code snippit that
    >does this?
    I think most people would recommend that you use a database for this.
    --
    Geoff Berrow
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs [url]http://www.ckdog.co.uk/rfdmaker/[/url]
    Geoff Berrow Guest

  3. #3

    Default Re: Newbe php help

    Geoff Berrow wrote:
    > I noticed that Message-ID: <vnlg8c4cao4940news.supernews.com> from
    > eboogyman contained the following:
    >
    >
    >>I want to save the firstname, lastname and address appended to a file in say
    >>results.txt, in the below format 1 entry per line...
    >>
    >>MyFirstname,MyLastname,MyAddress
    >>
    >>I know this is probebly easy, can someone help me with a code snippit that
    >>does this?
    >
    >
    > I think most people would recommend that you use a database for this.
    I think that most people would recommande that you use a thermonuclear
    balistic weapon to kill a fly.

    <op>
    If you only have this to store, no relation to other data, and not too
    much lines to store, the text file is a perfectly valid solution, and
    probably fastest espacially in share hosting environnement.
    </op>

    Bruno

    Bruno Desthuilliers Guest

  4. #4

    Default Re: Newbe php help

    eboogyman wrote:
    > I have an HTML form with Firstname, lastname, address
    >
    > I want to save the firstname, lastname and address appended to a file in say
    > results.txt, in the below format 1 entry per line...
    >
    > MyFirstname,MyLastname,MyAddress
    >
    > I know this is probebly easy, can someone help me with a code snippit that
    > does this?
    >
    You'll learn nothing by just copying some code you don't understand.

    A few tips :

    The form should call a php script in it's 'action' attribute, and the
    'GET' or 'POST' method (prefer POST).

    In the php script that's called, you can retrieve the 'firstname',
    'lastname' and 'address' inputs values in the $_GET or $_POST array :
    eg : $firstname = $_POST['firstname']

    But you should first check that this value is set with isset() :
    if (isset($_POST['firstname']) {
    $firstname = $_POST['firstname'];
    }
    else {
    // handle the error
    }

    You know have to append this to the file. This implies opening the file
    in append mode, writing to the file, and closing the file. fopen(),
    fwrite() and fclose() are your friends :

    [url]http://fr2.php.net/manual/en/function.fopen.php[/url]
    [url]http://fr2.php.net/manual/en/function.fwrite.php[/url]
    [url]http://fr2.php.net/manual/en/function.fclose.php[/url]

    typically, it would do something like :

    $file = fopen('path/to/filename.ext', 'a');
    if ($file == false) {
    // could not open the file, so handle the error
    }
    else {
    $buffer = "what you have to write\n";
    $written = fwrite($file, $buffer);
    fclose($file);

    // check everything is ok
    if ($written != strlen($buffer)) {
    // oops, there was a problem
    }
    }

    To write the three informations on a single line, the simplest thing to
    do is to concat them. Now dont forget that you may want to retrieve the
    info, and know how to split the line into parts. The usual way is to
    insert a separator before the fields. It's best to use a character (or
    string) that has very few chances to be in a name or adress. Something
    like a pipe ('|'), or a combination of unusual characterd (like '%%%')
    should do it.

    Now a last few things that might be useful to know:
    - with something based on the above code, you won't check if there are
    doubles in the persons list
    - if you've got a problem when writing the file, it will be somewhat
    messed up. It might be useful to backup the file before writing to it.
    - the process (here the web server running PHP) must have write access
    to the file
    - if you don't want every script kiddie and its sister to do anything to
    your file, you'd better protect it (this is usually done by some web
    server conf file - usually named '.htaccess' if the server is Apache).

    With all this and the PHP manual, there is no reason you couldn't write
    the code by yourself !-)

    HTH
    Bruno





    Bruno Desthuilliers Guest

  5. #5

    Default Re: Newbe php help

    Nice explaination, Bruno. Very helpful.

    Thanks from another newbie.

    Paul


    Paul James Guest

Similar Threads

  1. Newbe needs help
    By Sadman in forum PERL Beginners
    Replies: 6
    Last Post: August 17th, 07:27 PM
  2. newbe
    By Artemio in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 19
    Last Post: September 27th, 05:30 PM
  3. Newbe needs help.
    By bredal Jensen in forum ASP.NET Web Services
    Replies: 3
    Last Post: July 7th, 03:13 PM
  4. newbe background
    By Québec in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 19th, 07:42 PM
  5. newbe THANKS
    By Clif Gallagher in forum Linux Setup, Configuration & Administration
    Replies: 1
    Last Post: July 14th, 06:46 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139