Jump to content

echo field inputs to a txt file


filly00

Recommended Posts

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!
Link to comment
Share on other sites

Here's a short script to get you going:
[code]<?php
$fp = f open('textfile.txt','w');
f write($fp,"motd[]=\n{\n" . '"' . $_POST['fields1' . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};");
f close($fp);
?>[/code]
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
Link to comment
Share on other sites

[!--quoteo(post=375097:date=May 18 2006, 07:43 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 18 2006, 07:43 PM) [snapback]375097[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Here's a short script to get you going:
[code]<?php
$fp=f open('textfile.txt','w');
f write($fp,"motd[]=\n{\n" . '"' . $_POST['fields1' . '",' . "\n" . '"' . $_POST['field2'] . '",' . "\n};");
f close($fp);
?>[/code]
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>
Link to comment
Share on other sites

Quick & dirty code:
[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>[/code]

Ken
Link to comment
Share on other sites

<?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]
Link to comment
Share on other sites

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

[strike]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.[/strike]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.