+ 1

Why is this not writing the file please?

The contents are displaying correctly in the text area, but the contents are erased on submit rather than being overwritten by new content. Should there be an action attribute and if so what should it be? An ideas how I can get this to work please? <form method=post> <textarea cols="45" rows="20" name="txt"> <?php //Display current contents of file (notes.txt) into editable textarea box $file = readfile("notes.txt"); ?> </textarea> <?php //Rewrite file (notes.txt) with newly edited text $myfile = fopen("notes.txt", "w"); fwrite($myfile, $_POST[txt]); fclose($myfile); ?> <input type="submit" value="Save Your Notes"> </form>

12th Jul 2019, 2:58 PM
Vince Warner
Vince Warner - avatar
8 Antworten
+ 8
Hello Vince Warner, Well, this is just what I observed from the code, not a solution, but hopefully it might give you an idea where to go ... #praying ... You read the contents of "notes.txt" into a variable <$myfile>. But you didn't output this variable in between <textarea> and </textarea>, I'm not sure if it was intentional, I just thought maybe `echo $myfile;` in there ... In the second PHP code block you rewrite the "notes.txt" file with the content of an element from the $_POST array ... But see here ... `$_POST[txt]` I was thinking you probably meant `$_POST['txt']` (get an element from $_POST array having key 'txt') or otherwise `$_POST[$txt]` where $txt contains a string value (element key). But my primary concern is ... where did that $_POST[txt] come from, as I don't see any HTML input by that name in the code - so where did that come from? Elaborate more please ...
12th Jul 2019, 3:59 PM
Ipang
+ 2
To add to ipang's answer, you might want to check the file permissions, meaning on a Unix based system, do you have the autorisation to write in this directory?
12th Jul 2019, 4:09 PM
Drax
Drax - avatar
+ 2
Try adding, between php quotes ini_set('display_errors', 1); It should show you some more info.
12th Jul 2019, 4:21 PM
Drax
Drax - avatar
+ 2
You're welcome Vince Warner, I'm glad if it could help : )
12th Jul 2019, 6:06 PM
Ipang
+ 2
Congratulations Vince Warner! 👏 Yes, that "txt"part must've skipped my observation : ) TBH I've no experience writing plugins, I would appreciate if you care to share a bit about your project, must be something worth learning 👍
13th Jul 2019, 9:55 AM
Ipang
+ 2
To be honest this particular project is probably better called a widget than a plugin. It just adds a note taking system to the admin page of the membership site. The admin page is already set up so that users can add their own widgets if they know how to code them. There wasn't much integration required. As I get more experienced I have some "real" plugin projects I would like to tackle.
13th Jul 2019, 8:46 PM
Vince Warner
Vince Warner - avatar
+ 1
Putting single quotes around txt solved it. txt occurs here in the HTML <textarea cols="45" rows="20" name="txt"> Thank you for offering helpful comments and especially Ipang for providing the solution.
12th Jul 2019, 5:35 PM
Vince Warner
Vince Warner - avatar
+ 1
I've now got my plugin working just the way I wanted. I've installed it on two of my membership sites. This is my first ever PHP project, so I'm really pleased Thank you for your help.
13th Jul 2019, 8:27 AM
Vince Warner
Vince Warner - avatar