Reputation: 31
So for a few days now i have been looking through forum posts on how to make this work. I have found many post that were helpful. And had working Answers, but every time i tried it it didn't want to work for me. Now most of these post i saw were 2-5 years old. So i though to make a new one i thought to make a new one also since i don't really know any php, but i am trying to learn and so adapting the php is quite hard since i really know how to read it. so here is my code and some pics, also the links to the post i have looked at.BTW this is for a wiki project for a game called World At Arms, go check it out it is fun.
HTML Code:
<form action="totxt.php?saving=1" id="contact_form" method="post">
First Name:
<input type="text" name="first" id="first" value="First Name" onblur="if(this.value == '') { this.value = 'First Name'; }" onfocus="if(this.value == 'First Name') { this.value = ''; }" />
<br /> Last Name:
<input type="text" name="last" id="last" value="Last Name" onblur="if(this.value == '') { this.value = 'Last Name'; }" onfocus="if(this.value == 'Last Name') { this.value = ''; }" />
<br /> In Game Name:
<input type="text" name="ing" id="ign" value="In Game Name" onblur="if(this.value == '') { this.value = 'In Game Name'; }" onfocus="if(this.value == 'In Game Name') { this.value = ''; }" />
<br /> Current Level:
<input type="text" name="level" id="level" value="Current Level" onblur="if(this.value == '') { this.value = 'Current Level'; }" onfocus="if(this.value == 'Current Level') { this.value = ''; }" />
<br /> Glory:
<input type="text" name="glory" id="glory" value="Glory" onblur="if(this.value == '') { this.value = 'glory'; }" onfocus="if(this.value == 'glory') { this.value = ''; }" />
<br /> Message:
<br />
<textarea name="message" id="message" rows="10" cols="50"></textarea>
<br />
<input value="Submit" type="submit" />
</form>
So This is the Html code i have smashed together from post i have found. it is messy and i'm pretty sure there are some chucks that can be taken away but i'm no coding mastermind.
The PHP Code i have gathered together:
<?php
$first = $_POST["first"];
$last = $_POST["last"];
$ign = $_POST["ign"];
$level = $_POST["level"];
$glory = $_POST["glory"];
$message = $_POST["message"];
$text = "NAME: $first $last <br>
In Game Name: $ign<br>
Current Level: $company<br>
Glory: $glory<br>
MESSAGE: $message<br><hr><br><br><br>";
$file = "data.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $text) or die("Couldn't write values to file!");
fclose($file);
?>
<meta http-equiv="refresh" content="0; url=Success.html" />`
So this is the error i get when i hit submit:
Couldn't open data.txt for writing!
The links i used to get this far.
1: Get user input from form, write to text file using php
2: https://forums.digitalpoint.com/threads/save-php-form-data-to-a-txt-file.757223/
3: PHP write file from input to txt
4: There was another one but i cant seem to find the link.
Upvotes: 1
Views: 3163
Reputation: 77
What System are you on? On Unix (Linux, OSX) you can try chmod -R 777
on the folder you are working on. This should help.
Little Notice: your line
fclose($file);
will throw an error, you need to fclose the resource, not the file!
fclose($fp);
Edit: as mentoined by bishop (and yes it´s so right!) setting chmod -R 777
is only for testing. See Ronny Coolen answer, for setting the right permissions.
Upvotes: 0
Reputation: 36
Double check and see if the "data.txt" file is in the same folder as the script. If it is in the same folder, then look at these comments php-Error: Server error: couldn't open file from this question from stackoverflow user "CCA" see if this solves your problem.
Solutions might be:`
$file = "[full directory to]"/data.txt;
OR
$file = realpath('data.txt');
Upvotes: 0
Reputation:
There is one undefined variable $company
.
also your current directory may be read only . if your in localhost try to change web servers directory properties.
if your code is hosted online then try to change chmod 777
of your current directory.
also close your file pointer like fclose($fp)
Upvotes: 0
Reputation: 170
Your file is not writeable. This happens (on unix-like systems) when your web server user has no write permission on the file (or on the directory when creating a new file).
If your code is on a shared hosting platform, it is possible that you don't have the permissions to let PHP write to files, because of security.
Otherwise, try this on the command line
If the file exists:
chmod 666 data.txt
If the file does not exist:
chmod 777 <directory>
But take note: chmod 777 on directories lets everyone logged into the server write new files. You might want to rethink this. It creates a security issue.
A better plan of action is to change the user that is the owner of the file, or directory with the user of the web server (you will have to look that one up, on debian systems it is default www-data
, on other systems it might be httpd
or apache
, depending on the configuration. It might even be your username on the server. Ask your system administrator.
chown <user> <file>
chown <user> <directory>
When the directory or file is owned by the web server user, PHP can write to the file.
EDIT 1: Another problem (credits to fred-ii) is that your POST array is incorrect.
In your HTML code, there is a field <input name="ing" id="ign" ...>
The name
value of the input field is used for the PHP array index. So you need to change:
<input name="ing" ...
to <input name="ign" ...
or $_POST['ign']
to $_POST['ing']
EDIT 2: Also, (credits to pictus): you should use the correct parameter for fclose
:
fclose($fp);
Upvotes: 2
Reputation: 413
Some mistakes:
$ign = $_POST["ign"];
is not the same as name="ing"
, it should be name="ign"
also make sure you have permission to write the file.
EDIT: it seems Fred -ii- already posted this in the comment already, Didn't check the comment first. Credit goes to Fred -ii-.
Upvotes: 2
Reputation: 1
make sure that the path of the file is correct
$fp = fopen("$file","a+");
Upvotes: -3