+ 1
this input form wont write to the text folder
for some reason that fwrite() isnt doing what its supposed to do and i dont know why https://code.sololearn.com/wFMKs8aGzFO8/?ref=app
33 Answers
+ 2
that is what it is, i just tried to make a file with the .php extension and only gave it the option of
<?php
echo "hello world";
?>
and when i opened it in the browser instead of just reading hello world it read the entire text, php tags and all so its that the browser isnt carrying out my php is the issue
+ 1
I hope you're not trying to do this in SoloLearn, because storing permanent file is not supported.
I noticed your form was supposed to send the data to a file 'users8.php' but here I see you put the code to write the file 'logins.txt' within this very code. So I need to confirm - is this in one file, or the PHP code is actually in 'users8.php'?
Also quoting your question, "this input form won't write to the text folder". Did you mean to write text file 'logins.txt' in current folder, or did you mean to write to text file 'logins.txt' in a folder named 'text'?
+ 1
i meant it wont write to logins.txt an no im not writing this in solo learn i just pulled it from my servers /var/www/html directory...where i have stored user8.php as well as logins.txt and i camt figure why the fwrite wont conform
+ 1
and im not sure if i understand u when u say send the data to users8.php...the data is intended to go to logins.txt but problem is if i put logins.txt in the action attributes value then it leads the user to a blank pg which is indeed the logins.txt file however it writes nothing to the file so i placed user8.php in the actions value to make the code roundabout to the same pg; hence the name of the current file in directory is as well users8.php so its as if it gives a simple refresh of the page which is fine, ill deal with where i will send the user after i figure how to handle this data hence one step.at a time....and upons the pages round about of the same page it is supposed to drop the input data off to logins.txt whivh is whats failing to happen as the users input is not writing to the txt file......i hope i didnt confuse you...
+ 1
Try to fix these lines first, you shouldn't have put '#x27; before function name upon a call to fopen(), fwrite() or fclose(). And you should pass file handle (that was returned by fopen()) when calling fwrite() and fclose(), shouldn't have passed the filename.
* Your code:
$logs=$fopen($filename, "w+");
$fwrite($filename, $data);
$fclose($filename);
* Should be:
$logs = fopen($filename, "w+");
// Syntax: fopen(filename, mode, [include_path], [context])
fwrite($logs, $data);
// Syntax: fwrite(file, string, [length])
fclose($logs);
// Syntax: fclose(file)
Also if you are submitting form data to the same file you don't need to specify 'action' attribute. Specify the 'action' attribute only when you will be sending the form data to another PHP file. Alternatively you can opt to use 'action' attribute but give it a blank value, e.g. <form action="">...</form>.
+ 1
No there's no need for <script> tag to use PHP.
Your call to fopen(), fwrite() and fclose() is still wrong. Look at my previous reply and edit the code again.
Can you tell me how you access the PHP page? I mean what address you use in browser. Are you doing this in a local server (your PC/laptop) or you hosted the website?
+ 1
im doing a portforward through a hosted website
+ 1
and i didnt understamd that [length] on your fwrite
+ 1
<?php
$filename = "logins.txt";
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$filename = "logins.txt";
$logs=fopen($filename, "w+", "
"http://c8191f11.ngrok.io/");
fwrite($filename, $data, [length]); // the length of what
fclose($filename);
}
http://c8191f11.ngrok.io/users18.php is where u will find me in the browser for now
+ 1
In syntax part? function parameters wrapped in [ ] brackets are optional, you can skip those parameters when you call the function and there will be no more problem.
fwrite(file, string, [length])
You can call fwrite function just passing the 'file' and 'string' arguments. You don't need to pass argument 'length'.
PHP file system functions reference:
https://www.w3schools.com/PHP/php_ref_filesystem.asp
+ 1
when i call the function??? is the function not already called when user presses the login/savedata button of the html?
+ 1
When you do
fopen($filename, "w+");
You are calling fopen function.
+ 1
i understand...but i just found something out, so i chaged the name of logins.txt to logins.php and i gave the fwrite function and echo before the text to see if the file would just simply echo the $data output to a php file, now i originaly had a logins.txt folder already made but when i changed it to logins.php i was utilizing fopen's create the folder if it does not exist function, hence i didnt create an already existing folder in the directory, and the error i recieved after sending the data and attempting to open logins.php was that the file did not exist so this informs me that there might not be a problem with my fwrite function but instead, my entire php script is not boeing carried out at all and i cant figure out why.....for some reason that php is just not registering
+ 1
i just installed php in my linux terminal and im gonna give it a second go round
+ 1
IT WORKING!!!!!!
+ 1
Well, if you insist that WAS the problem : )
0
Looks like we misunderstood each other here : )
What I meant by 'data' was the form data, the values written in the input boxes, not the <$data> variable. As I understand it, 'action' attribute of <form> is used to specify the file that handles the form data. So previously I thought you had 2 files, first one is this one you shared, and another one is the 'users8.php' file. So I thought you get input in this file and send (submit) the form data to 'users8.php' to be processed. But now I understand you are submitting the form data to the same file.
* Continued on next reply ...
0
WWOOOOOWWWWWW I DIDNT EVEN NOTICE ID PUT $ BEFORE THE FUNCTIONS, THATS SUPER EMBARRASING, IM AN IDIOT, LEMME REDRAFT THAT, ONE SEC
0
well☹️ no luck, i tried it with the file location apart of the open function, and without it and i removed the dollar signs from the functions, nothing worked....u dont think i would have to include a script src to refer to the php to use do u? for ex.
in my html
<script src= --! php version-- >
<?php
//code
?> <
/script>
because im noticing none of the php is activating, is it because the webpage doesnt know its supposed to use php??? this was the entire document //practice document
https://code.sololearn.com/W9Ye9e2e77bu/?ref=app
0
Yes, keep me posted for news. And also see my suggestion about the code, as I see in your most recent code you still isn't fully following what I suggested : )