filly00 Posted May 19, 2006 Share Posted May 19, 2006 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.txtmotd[]={"<form field one echo here>","<form field two echo here>",};----end of filecan 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! Quote Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/ Share on other sites More sharing options...
kenrbnsn Posted May 19, 2006 Share Posted May 19, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/#findComment-37036 Share on other sites More sharing options...
filly00 Posted May 19, 2006 Author Share Posted May 19, 2006 [!--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> Quote Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/#findComment-37097 Share on other sites More sharing options...
kenrbnsn Posted May 19, 2006 Share Posted May 19, 2006 Quick & dirty code:[code]<?phpif (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 Quote Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/#findComment-37163 Share on other sites More sharing options...
filly00 Posted May 19, 2006 Author Share Posted May 19, 2006 <?phpif (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] Quote Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/#findComment-37313 Share on other sites More sharing options...
filly00 Posted May 20, 2006 Author Share Posted May 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/9963-echo-field-inputs-to-a-txt-file/#findComment-37469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.