Jump to content


Photo

echo field inputs to a txt file


  • Please log in to reply
5 replies to this topic

#1 filly00

filly00
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 May 2006 - 12:22 AM

I'm wanting to make a script that will collect input typed into a form field, then echo it in a text file. Although the information that will be echoed will need to be in an assigned area, EG

----textfile.txt

motd[]=
{
"<form field one echo here>",
"<form field two echo here>",
};


----end of file

can anyone start me off with this script.. for the php file to fetch the input from the fields and teach me how to echo it.

I'm new to php so try not to be to complicated although im not a complete noob... i know quite abit c++ and i notice php is quite similar

Thanks in advance for help with this!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 12:43 AM

Here's a short script to get you going:
<?php
$fp = f open('textfile.txt','w');
f write($fp,"motd[]=\n{\n" . '"' . $_POST['fields1' . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};");
f close($fp);
?>
This assumes that your form uses method="POST", if it uses "GET", change all $_POST to $_GET.

Note: Please remove the space between the "f" and the rest of the function.

Ken

#3 filly00

filly00
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 May 2006 - 08:18 AM

[!--quoteo(post=375097:date=May 18 2006, 07:43 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 18 2006, 07:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here's a short script to get you going:
<?php
$fp=f open('textfile.txt','w');
f write($fp,"motd[]=\n{\n" . '"' . $_POST['fields1' . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};");
f close($fp);
?>
This assumes that your form uses method="POST", if it uses "GET", change all $_POST to $_GET.

Note: Please remove the space between the "f" and the rest of the function.

Ken
[/quote]


Thanks for the reply, how can I go about connecting this with the form.

<form method="POST" action="--WEBBOT-SELF--">
<p><input type="text" name="field1" size="20"></p>
<p><input type="text" name="field2" size="20"></p>
</form>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 02:13 PM

Quick & dirty code:
<?php
if (isset($_POST['submit'])) {
    $ho = (file_exists('textfile.txt'))?'a':'w'; // open with append if file exists
    $fp=f open('textfile.txt',$ho);
    f write($fp,"motd[]=\n{\n" . '"' . $_POST['field1'] . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};"); 
    f close($fp);
}
?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1" size="20"><br>
<input type="text" name="field2" size="20"><br>
<input type="submit" name="submit" value="Send Information">
</form>
</body>
</html>

Ken

#5 filly00

filly00
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 May 2006 - 10:54 PM

<?php
if (isset($_POST['submit'])) {
$ho = (file_exists('textfile.txt'))?'a':'w'; // open with append if file exists
$fp=f open('textfile.txt',$ho);
f write($fp,"motd[]=\n{\n" . '"' . $_POST['field1'] . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};");
f close($fp);
}
?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1" size="20"><br>
<input type="text" name="field2" size="20"><br>
<input type="submit" name="submit" value="Send Information">
</form>
</body>
</html>

Hi ken, thanks! i ran that code, and the form works although i need to them make it open the textfile.txt as nothing shows when i hit send information. How can i do this...? thank you. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

[a href=\"http://www.clanshout.co.uk/code1.php\" target=\"_blank\"]http://www.clanshout.co.uk/code1.php[/a]

#6 filly00

filly00
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 May 2006 - 07:04 PM

ahhh, it would not make a file by its self but it soon worked after i made textfile.txt and gave it write access.

www.clanshout.co.uk/textfile.txt

As you can see each input is following on from the last, I need it to remove all the previous input and start again, in other words overwriting the current content, how do i go about doing this? thank you.

Ahhh I understand with the Append / W things, thats alright now, but how do can i show the file its just made right after submit is pressed.


all sorted, many thx ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users