Storing form field names in an array and writing values to a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul C-T

    Storing form field names in an array and writing values to a text file

    Hi,

    Am I trying to be too clever here?

    I am trying to write a PHP page to enable me to enter values into a form
    then write those values to a text file.
    I want to use the form & table that displays these fileds and values in
    different ways so am creating one page and parsing variables to it.
    The problem appears to be in trying to write the values to a text file. The
    page displays the field names correctly but when I submit the form and try
    to write the values to a text file, separated by # what I get is
    ############## but no values !!!

    Any help greatly appreciated .... Paul.


    Config.php:

    $filename = "./config.txt"
    $fieldnames = array ("business_name ", "business_addre ss", "business_phone ");
    // There are more but you get the idea

    // Firstly if the file exists read the existing values and store them in an
    array where $fieldname[$i] = $data[$i]

    if(file_exists( $filename))
    {
    $fp = fopen($filename , "r");
    $contents = fread($fp, filesize($filen ame));
    $data = explode("#", $contents, 16);
    for($i = 0; $i < 17; ++$i)
    {
    $fieldnames[$i] = $data[$i];
    }
    fclose($fp);

    // Display the form in update mode with current values as defaults

    $button = "update";
    include("./config_table.ph p");
    }

    elseif($submit == "save" || $submit == "update")
    {

    // The form has been submitted so write the values to the text file

    $fp = fopen($filename , "w+");
    for($i = 0; $i < 17; ++$i)
    {
    fwrite($fp, $data[$i] . "#"); // uses the # symbol to separate data
    items
    }
    fclose($fp);

    print '<SPAN CLASS="message" >Your configuration values have been ' .
    $submit . 'd successfully.</SPAN>';
    }

    else
    {

    // Display the form in install mode as this if the first time the file
    has been run

    $button = "save";
    include("./config_table.ph p");
    }



    Config_table.ph p:

    <FORM NAME="configura tion" METHOD="post" ACTION="config. php">

    <TABLE>

    <?php $f = 0; ?>

    <TR><TD>Busines s Name</TD>
    <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>

    <?php ++$f; ?>

    <TR><TD>Busines s Address</TD>
    <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>

    <?php ++$f; ?>

    <TR><TD>Busines s Phone</TD>
    <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>

    <TR><TH COLSPAN="2"><IN PUT TYPE="submit" VALUE="<?php echo $button;
    ?>">&nbsp;<INPU T TYPE="reset" VALUE="reset"></TH></TR></TABLE></FORM>


  • Paul C-T

    #2
    Re: Storing form field names in an array and writing values to a text file

    Forgot to mention in the config_table.ph p page I am using the VALUE="<?php
    echo $fieldnames[$f]; ?>" for testing only - saves me having to enter values
    all the time ... would otherwise be $data array.

    So in my text file I should expect to see
    "business_name# business_addres s#business_phon e# etc ...

    Paul.

    "Paul C-T" <paulcharltonth omson@hotmail.c om> wrote in message
    news:40f6b2e1$0 $4526$ed2619ec@ ptn-nntp-reader01.plus.n et...[color=blue]
    > Hi,
    >
    > Am I trying to be too clever here?
    >
    > I am trying to write a PHP page to enable me to enter values into a form
    > then write those values to a text file.
    > I want to use the form & table that displays these fileds and values in
    > different ways so am creating one page and parsing variables to it.
    > The problem appears to be in trying to write the values to a text file.[/color]
    The[color=blue]
    > page displays the field names correctly but when I submit the form and try
    > to write the values to a text file, separated by # what I get is
    > ############## but no values !!!
    >
    > Any help greatly appreciated .... Paul.
    >
    >
    > Config.php:
    >
    > $filename = "./config.txt"
    > $fieldnames = array ("business_name ", "business_addre ss",[/color]
    "business_phone ");[color=blue]
    > // There are more but you get the idea
    >
    > // Firstly if the file exists read the existing values and store them in[/color]
    an[color=blue]
    > array where $fieldname[$i] = $data[$i]
    >
    > if(file_exists( $filename))
    > {
    > $fp = fopen($filename , "r");
    > $contents = fread($fp, filesize($filen ame));
    > $data = explode("#", $contents, 16);
    > for($i = 0; $i < 17; ++$i)
    > {
    > $fieldnames[$i] = $data[$i];
    > }
    > fclose($fp);
    >
    > // Display the form in update mode with current values as defaults
    >
    > $button = "update";
    > include("./config_table.ph p");
    > }
    >
    > elseif($submit == "save" || $submit == "update")
    > {
    >
    > // The form has been submitted so write the values to the text file
    >
    > $fp = fopen($filename , "w+");
    > for($i = 0; $i < 17; ++$i)
    > {
    > fwrite($fp, $data[$i] . "#"); // uses the # symbol to separate data
    > items
    > }
    > fclose($fp);
    >
    > print '<SPAN CLASS="message" >Your configuration values have been ' .
    > $submit . 'd successfully.</SPAN>';
    > }
    >
    > else
    > {
    >
    > // Display the form in install mode as this if the first time the file
    > has been run
    >
    > $button = "save";
    > include("./config_table.ph p");
    > }
    >
    >
    >
    > Config_table.ph p:
    >
    > <FORM NAME="configura tion" METHOD="post" ACTION="config. php">
    >
    > <TABLE>
    >
    > <?php $f = 0; ?>
    >
    > <TR><TD>Busines s Name</TD>
    > <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    > $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>
    >
    > <?php ++$f; ?>
    >
    > <TR><TD>Busines s Address</TD>
    > <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    > $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>
    >
    > <?php ++$f; ?>
    >
    > <TR><TD>Busines s Phone</TD>
    > <TD><INPUT TYPE="text" MAXLENGTH="255" SIZE="40" NAME="<?php echo
    > $fieldnames[$f]; ?>" VALUE="<?php echo $fieldnames[$f]; ?>"></TD></TR>
    >
    > <TR><TH COLSPAN="2"><IN PUT TYPE="submit" VALUE="<?php echo $button;
    > ?>">&nbsp;<INPU T TYPE="reset" VALUE="reset"></TH></TR></TABLE></FORM>
    >
    >[/color]


    Comment

    • Tim Van Wassenhove

      #3
      Re: Storing form field names in an array and writing values to a text file

      In article <40f6b2e1$0$452 6$ed2619ec@ptn-nntp-reader01.plus.n et>, Paul C-T wrote:[color=blue]
      > The problem appears to be in trying to write the values to a text file. The
      > page displays the field names correctly but when I submit the form and try
      > to write the values to a text file, separated by # what I get is
      > ############## but no values !!![/color]

      What is the setting for register_global s?

      If you do a little search on "missing values" in this newsgroup, you
      won't believe how many times this question has been answered. Can't
      believe ppl still bother to answer this question.

      --
      Tim Van Wassenhove <http://home.mysth.be/~timvw>

      Comment

      • Paul C-T

        #4
        Re: Storing form field names in an array and writing values to a text file

        Tim,

        Not everyone is as unhelpful as you ... I tried searching through the 1500
        messages but did not find the answer I needed ... hence the question.

        ;-)

        Paul.

        "Tim Van Wassenhove" <euki@pi.be> wrote in message
        news:2lns8vFeup n1U3@uni-berlin.de...[color=blue]
        > In article <40f6b2e1$0$452 6$ed2619ec@ptn-nntp-reader01.plus.n et>, Paul C-T[/color]
        wrote:[color=blue][color=green]
        > > The problem appears to be in trying to write the values to a text file.[/color][/color]
        The[color=blue][color=green]
        > > page displays the field names correctly but when I submit the form and[/color][/color]
        try[color=blue][color=green]
        > > to write the values to a text file, separated by # what I get is
        > > ############## but no values !!![/color]
        >
        > What is the setting for register_global s?
        >
        > If you do a little search on "missing values" in this newsgroup, you
        > won't believe how many times this question has been answered. Can't
        > believe ppl still bother to answer this question.
        >
        > --
        > Tim Van Wassenhove <http://home.mysth.be/~timvw>[/color]


        Comment

        • Tim Van Wassenhove

          #5
          Re: Storing form field names in an array and writing values to a text file

          In article <40f6d31b$0$200 $ed2619ec@ptn-nntp-reader03.plus.n et>, Paul C-T wrote:[color=blue]
          > Tim,
          >
          > Not everyone is as unhelpful as you ... I tried searching through the 1500
          > messages but did not find the answer I needed ... hence the question.[/color]

          Well, i went to google, search the groups

          comp.lang.php missing values. And the second hit already mentionned
          register_global s... Guess i was lucky :d

          --
          Tim Van Wassenhove <http://home.mysth.be/~timvw>

          Comment

          • CJ Llewellyn

            #6
            Re: Storing form field names in an array and writing values to a text file

            "Paul C-T" <paulcharltonth omson@hotmail.c om> wrote in message
            news:40f6b3fb$0 $4533$ed2619ec@ ptn-nntp-reader01.plus.n et...[color=blue]
            > Forgot to mention in the config_table.ph p page I am using the VALUE="<?php
            > echo $fieldnames[$f]; ?>" for testing only - saves me having to enter[/color]
            values[color=blue]
            > all the time ... would otherwise be $data array.
            >
            > So in my text file I should expect to see
            > "business_name# business_addres s#business_phon e# etc ...[/color]

            As a piece of general advice, you'd be much better off using a XML file
            format for this.



            Comment

            Working...