By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,508 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,508 IT Pros & Developers. It's quick & easy.

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

P: n/a
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_address", "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($filename));
$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.php");
}

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.php");
}

Config_table.php:

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

<TABLE>

<?php $f = 0; ?>

<TR><TD>Business 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>Business 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>Business 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"><INPUT TYPE="submit" VALUE="<?php echo $button;
?>">&nbsp;<INPUT TYPE="reset" VALUE="reset"></TH></TR></TABLE></FORM>
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Forgot to mention in the config_table.php 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_address#business_phone # etc ...

Paul.

"Paul C-T" <pa*****************@hotmail.com> wrote in message
news:40**********************@ptn-nntp-reader01.plus.net...
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_address", "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($filename));
$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.php");
}

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.php");
}

Config_table.php:

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

<TABLE>

<?php $f = 0; ?>

<TR><TD>Business 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>Business 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>Business 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"><INPUT TYPE="submit" VALUE="<?php echo $button;
?>">&nbsp;<INPUT TYPE="reset" VALUE="reset"></TH></TR></TABLE></FORM>

Jul 17 '05 #2

P: n/a
In article <40**********************@ptn-nntp-reader01.plus.net>, Paul C-T wrote:
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 !!!


What is the setting for register_globals?

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>
Jul 17 '05 #3

P: n/a
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" <eu**@pi.be> wrote in message
news:2l************@uni-berlin.de...
In article <40**********************@ptn-nntp-reader01.plus.net>, Paul C-T

wrote:
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 !!!


What is the setting for register_globals?

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>

Jul 17 '05 #4

P: n/a
In article <40*********************@ptn-nntp-reader03.plus.net>, Paul C-T wrote:
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.


Well, i went to google, search the groups

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

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #5

P: n/a
"Paul C-T" <pa*****************@hotmail.com> wrote in message
news:40**********************@ptn-nntp-reader01.plus.net...
Forgot to mention in the config_table.php 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_address#business_phone # etc ...


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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.