Bounty Posted February 17, 2010 Share Posted February 17, 2010 Hello...im trying to make a script that will add a input text field to my page and a submit button witch would save all data in the textfield as "somename.txt"...this is all i could get can you help me out here? :/ <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <table align="center"> <tr> <td> <textarea name="textfield" cols="45" rows="5" id="textfield">Text</textarea></td> </tr> <tr> <td align="center"> <input name="submit" type="submit" id="button" value="Submit" onClick=""></td> </tr> </table> </body> <?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "blahhh"; fwrite($fh, $stringData); fclose($fh); ?> </html> Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/ Share on other sites More sharing options...
sader Posted February 17, 2010 Share Posted February 17, 2010 Hmm well if u want to display text in your textarea from your file one way for it would be <textarea> <?php //fopen your file for reading and echo it's content ?> </textarea> Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013921 Share on other sites More sharing options...
LeadingWebDev Posted February 17, 2010 Share Posted February 17, 2010 Hi, u need to just store data? try this: <?php if(!$_POST) { ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <table align="center"> <form name="text_field_store_data" action="<?=$_SERVER['PHP_SELF']?>" method="POST"> <tr> <td> <textarea name="textfield" cols="45" rows="5" id="textfield">Text</textarea></td> </tr> <tr> <td align="center"> <input name="submit" type="submit" id="button" value="Submit" onClick=""></td> </tr> </form> </table> </body> <? } else { $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $_POST['textfield']; // may u protect the string? if(!fwrite($fh, $stringData)) { echo "error appear while trying to store data"; }else{ fclose($fh); echo "Successfuly stored"; echo "<meta http-equiv='REFRESH' content='2;url=$_SERVER[REQUEST_URI]'>"; } } ?> </html> why we do a checking if(!$_POST) if not post in other words, if visitor didnt clicked on Submit he will see only 1st part of script, that contains HTML form only, else he will not see a form, but php part will execute and store data, if script cant open file, error appears, if script cant save data for any reason the error will apeare, if all operation success - Successfuly stored, and will redirect visitor to the same form. Hope it was helpful. Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013932 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 Here is the LINK What did i do wrong? I used LeadingWebDev's code...:/ Thanks sader that would be helpful too.. Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013953 Share on other sites More sharing options...
LeadingWebDev Posted February 17, 2010 Share Posted February 17, 2010 short_open_tags = On its works. try configuring your php.ini. Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013955 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 Php.ini ??? Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013958 Share on other sites More sharing options...
LeadingWebDev Posted February 17, 2010 Share Posted February 17, 2010 php configuration file, located in PHP directory Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013963 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 Im not sure i have that..all my php scripts i tested on this host worked just fine..:/ Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013968 Share on other sites More sharing options...
LeadingWebDev Posted February 17, 2010 Share Posted February 17, 2010 try setting up your own server on localhost. apache 2.2 php 5.X mysql 5.X Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013972 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 Why this cant be done without all these things :/ It should be simple...script.php/html and done.. Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013975 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 Sorry about that..it works fine now..i noticed the problem..thanks guys..i will experiment a little more and if i have some more questions i will post here.thanks again Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013979 Share on other sites More sharing options...
LeadingWebDev Posted February 17, 2010 Share Posted February 17, 2010 SOLVED so. Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013984 Share on other sites More sharing options...
Bounty Posted February 17, 2010 Author Share Posted February 17, 2010 So far xDD Just another question...i created another text field to be input for a name of the file...the string looks like this: $myFile = $_POST['name']; And it works but what to use to add .txt to the name of file ? Sorry for bothering :/ Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013993 Share on other sites More sharing options...
sader Posted February 17, 2010 Share Posted February 17, 2010 $myFile = $_POST['name'].".txt"; Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1013995 Share on other sites More sharing options...
Bounty Posted February 20, 2010 Author Share Posted February 20, 2010 Heh and now how to display text in text box? //fopen your file for reading and echo it's content Link to comment https://forums.phpfreaks.com/topic/192429-some-coding-help/#findComment-1015098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.