Page 1 of 1

write form input to file

Posted: Sun May 25, 2008 4:51 pm
by dr zoidberg
Hello

This is my first post here so apologies if i do any thing wrong, i have read some of the highlighted posts but not all yet.

I am very much a html, php, mysql newbie, reading my way through online tutorials and puting things together as i go.

the problem i currently have is with taking the user input from a form and writing this input to a file.

My form code is as follows.

Code: Select all

 
<form method="post" action="<?php //echo ($_SERVER['PHP_SELF']);  ?>">
    <div align="center">Player Name  :
      <input type="text" size="12" maxlength="25" name="playername">
      <br />
    Alliance     :
    <input type="text" size="12" maxlength="25" name="alliancename">
    <br />
    Galaxy       :
    <input type="text" size="12" maxlength="1" name="galaxyname">
    <br />
    Solar System :
    <input type="text" size="12" maxlength="3" name="ssname">
    <br />
    Slot         :
    <input type="text" size="12" maxlength="2" name="slotname">
    <br />
    Phalanx Level:
    <input type="text" size="12" maxlength="2" name="phalanxname">
    <br />
    <br />
    <input type="submit" value="Submit Info" name="enter">
    </form>
 
The form appears on the page as i want it (ish), and when the submit button is pressed the current page is re-displayed with the fields empty, this is the out come i want. (not that means the code is correct :))

I want to then write the input from the 6 fields, playername, alliance, etc etc, to a txt file in the same dirctory as the php file containing the form is located.

I have tried several ways of doing this but my txt file remains empty.

This should take the user input and assign to variables, then run storeinfo function.

Code: Select all

 
<?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

Code: Select all

 
<?php
function storeinfo(){
$myFile = "store.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$playername\n";
fwrite($fh, $stringData);
$stringData = "$alliancename\n";
fwrite($fh, $stringData);
$stringData = "$galaxyname\n";
fwrite($fh, $stringData);
$stringData = "$ssname\n";
fwrite($fh, $stringData);
$stringData = "$slotname\n";
fwrite($fh, $stringData);
$stringData = "$phalanxname\n";
fwrite($fh, $stringData);
fclose($fh);
}
?>
 
This has been put together with different parts of code and from tutorials i have found online, but some were i have something wrong,
I think i have over complicated things some where along the lines,

Bothe the

<?php
?>

snipets are in the main php file containing the form,

Am i going in the right direction or do i have things completly wrong / over complicated ?

Can some one offer some advice please?

Thank You

Re: write form input to file

Posted: Sun May 25, 2008 8:58 pm
by deeessay
You didn't say how you would like the info to be written in the .txt file..... I can't write a code for you.

But hope this helps:

http://www.tizag.com/phpT/filewrite.php

Re: write form input to file

Posted: Sun May 25, 2008 9:16 pm
by yacahuma

Code: Select all

 
file_put_contents  ( 'mydata.txt'  , implode(',',$_POST));