Newbe php help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eboogyman

    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,MyL astname,MyAddre ss

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



    thanks.



  • Geoff Berrow

    #2
    Re: Newbe php help

    I noticed that Message-ID: <vnlg8c4cao4940 @news.supernews .com> from
    eboogyman contained the following:
    [color=blue]
    >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,My Lastname,MyAddr ess
    >
    >I know this is probebly easy, can someone help me with a code snippit that
    >does this?[/color]

    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 http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Bruno Desthuilliers

      #3
      Re: Newbe php help

      Geoff Berrow wrote:[color=blue]
      > I noticed that Message-ID: <vnlg8c4cao4940 @news.supernews .com> from
      > eboogyman contained the following:
      >
      >[color=green]
      >>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,M yLastname,MyAdd ress
      >>
      >>I know this is probebly easy, can someone help me with a code snippit that
      >>does this?[/color]
      >
      >
      > I think most people would recommend that you use a database for this.[/color]

      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

      Comment

      • Bruno Desthuilliers

        #4
        Re: Newbe php help

        eboogyman wrote:[color=blue]
        > 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,MyL astname,MyAddre ss
        >
        > I know this is probebly easy, can someone help me with a code snippit that
        > does this?
        >[/color]

        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 :





        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





        Comment

        • Paul James

          #5
          Re: Newbe php help

          Nice explaination, Bruno. Very helpful.

          Thanks from another newbie.

          Paul


          Comment

          Working...