What I want to do is write the input from the form into a separate text file on submission. Please note that this form cannot be changed.
The question is, how can I save the input in a separate TXT file? This is the form:
<form method="post"> <textarea id="name"></textarea> <textarea id="message"></textarea> <input type="submit" name="submit"> </form>
This is what I try to do:
<?php if(isset($_POST['submit'])) { $name = $_POST['#name']; $message = $_POST['#message']; $text = $name . "," . $message . "\n"; $fp = fopen('questions.txt', 'a+'); if(fwrite($fp, $text)) { // ini_set('display_errors', 1); // ini_set('display_startup_errors', 1); // error_reporting(E_ALL); echo 'Thank you!'; } fclose ($fp); } ?>
Please note that in many solutions I've read here, all form inputs have names (i.e. name='message'
). Unfortunately, my form cannot be changed, which is why I tried (unsuccessfully) using $name=$_POST['#name']
hoping to use the input ID (note the #) .
Can I save the input to a text file without modifying the form? The file questions.txt exists and is writable 777.
Thanks.
P粉7432884362023-09-10 17:24:38
There is a problem directory here.
It's very simple to check this by using the provided web developer tools to view the data sent by the browser or by pointing the form to, for example:
<pre> <?php print_r($_POST); ?> </pre>
At least in Firefox, the browser does not send any data back to the server except for the submit button. If you can't rewrite the HTML page using a form (or add Javascript), then this will never work.
Put this aside for now...
Yes, but this is a bad idea. There are many reasons why we use databases to store data.
This is always a bad idea. The same goes for allowing PHP to write to files in the web root.