writing out to file

Associate
Joined
18 Oct 2002
Posts
710
Location
Somerset
Should this work?

I have a page up that has several input boxes all controlled by 1 submit button,

when everything is submitted each entry from each box is put through $POST and assigned to separate variables.

then a function runs that takes these variable values and writes them to a txt file.

But its not working.

Here is a cut down version of the code to include 1 entry instead of the 5 or 6 i am trying to use.

Form on page.
Code:
<title>Enter info</title>
	<form method="post" action="<?php echo $PHP_SELF;?>">
	<div align="center">Player Name  :
	  <input type="text" size="12" maxlength="25" name="playername">

<input type="submit" value="Submit Info" name="enter">
    </form>

Takes submitted info from form and assigns to variables then runs storeinfo function.
Code:
<?php
$playername = $_POST["playername"];
if (!isset($_POST['enter']))
{
storeinfo();
}
?>

writes these variables to txt file
Code:
<?php
function storeinfo(){
//open store file in append mode
$myFile = "store.txt";
$fh = fopen($myFile, 'a') or die("can't open file");

//add new moon details to the end
$stringData = ".$playername";
fwrite($fh, $stringdata);
fclose($fh);
}
?>

I was thinking that the 2nd section was not being called but i think it is because the txt file contains data if i write some thing different between the "" in the 3rd section and as far as i understand it the storeinfo function will only run if it is called? which the 2nd section does
 
if (!isset($_POST['enter']))

Get rid of the !

Thanks but that didn't work,

All i am getting in the txt file is a row of squares which is from the \n at the end of each

$stringData = ".$playername\n";

the \n isnt in the snippet i posted but should have been. used to seperate the inputs.

$playername is not being convertedinto $stringData as far as i can see.
 
OK just realised i didn't even post all the code up properly.

form
Code:
<form method="post" action="<?php echo $PHP_SELF;?>">
	<div align="center">Player Name  :
	  <input type="text" size="12" maxlength="25" name="playername">
	  <input type="submit" value="Submit Info" name="enter">
    </form>

takes form entered info, asigns to variables, runs storeinfo function
Code:
<?php
if (isset($_POST['submit']))
{
$playername = $_POST["playername"];
$alliancename = $_POST["alliancename"];
$galaxyname = $_POST["galaxyname"];
$ssname = $_POST["ssname"];
$slotname = $_POST["slotname"];
$phalanxname = $_POST["phalanxname"];
storeinfo();
}
?>

storeinfo function which should write the info to a txt file
Code:
<?php
function storeinfo(){
//open store file in append mode
$myFile = "store.txt";
$fh = fopen($myFile, 'a') or die("can't open file");

//add new moon details to the end
$stringData = "$playername";
fwrite($fh, $stringdata);
fclose($fh);
}
?>

That is what is not working at some point,

Is it possible to make that work or is there a easy way to take user form input and append it to a txt file?
 
storeinfo function which should write the info to a txt file
Code:
<?php
function storeinfo(){
//open store file in append mode
$myFile = "store.txt";
$fh = fopen($myFile, 'a') or die("can't open file");

//add new moon details to the end
$stringData = "$playername";
fwrite($fh, $stringdata);
fclose($fh);
}
?>
The variable $playername is out of the scope of the storeinfo() function - either declare your function to take a parameter, i.e.
Code:
function storeinfo($stringdata) { ... }
or use the global keyword to access the variable.
Code:
$stringData = "$playername";
What is the point of that line...? :p
 
The variable $playername is out of the scope of the storeinfo() function - either declare your function to take a parameter, i.e.
Code:
function storeinfo($stringdata) { ... }
or use the global keyword to access the variable.
Code:
$stringData = "$playername";
What is the point of that line...? :p

Ok i added

global $playername, etc, etc, etc;

to the function,

still not working,

as for

$stringData = "$playername";

see what you mean :D bit pointless, just change the line under from

fwrite($fh, $stringData);

to

$stringData = "$playername\n";

Done that way as that is the way the guides i was following was doing it.

im still getting a blank file though,

i have a .php file that is the file loaded in the browser, it contains the form.

both the php sections above are also in this file,
Is this OK or should they be in a separate file?
is there a certain place with in the file they should be?
im not convinced that everything is being called correctly?
 
replace

PHP:
$myFile = "store.txt";

with

PHP:
$myFile = "./store.txt";

also check the file permissions - the script will need write access to the directory containing store.txt
 
replace

PHP:
$myFile = "store.txt";

with

PHP:
$myFile = "./store.txt";

OK changed that


also check the file permissions - the script will need write access to the directory containing store.txt

um, checking the permissions for the directory read only has a square in it not tick, click it to empty box and click apply, tell it to set for all files sub folders etc, click ok,

go back and check again and the square is back again :mad:
 
Back
Top Bottom