简体   繁体   中英

HTML Form save to server-side text file

All.

I've found many-a scripts which save form input to the local computer, IE downloading a text document with the submitted information, however I am trying to develop a html form which, when the 'submit' button is pressed, saves the text in the text field(s) as a single text document. The closest I've come is the following (Sorry, can't find where I got this code from originally. Hope that's okay :/)

<html>
<head>
<script>
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
$fileWrite = $_POST['fileWrite'];
}
if($fileWrite) {
$fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
</script>
</head>

<body>
<form id="some" name="someName" method="post" action="/../PastEntries/file.txt">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<input type="submit" value="submit" class="submitClass"/>
</form>
</body>
</html>

However, I get the error "Cannot POST /../www/file.txt"
I'm new to PHP, and have no idea how to fix this. I hope someone more experienced can help or provide an alternative!
Thanks in advance!

Not tested but you could do something like this. The form action is removed entirely so the form POSTS to the same page - the request is interpreted by PHP and the data written to a text file.

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        $filename=__DIR__ . '\\file.txt';
        file_put_contents( $filename, implode( PHP_EOL,$_POST ) . PHP_EOL, FILE_APPEND );
        exit();
    }
?>
<!doctype html>
<html>
    <head>
        <title>Save Form data to text file</title>
    </head>
    <body>
        <form id="some" name="someName" method="post">
            <input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
            <input type="submit" value="submit" class="submitClass"/>
        </form>
    </body>
</html>

I had was fiddling around with your code to see if i can make it work. Below is the fixed version.

<html>
<head>

<?php

$fileWrite = '';    
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
$fileWrite = $_POST['fileWrite'].PHP_EOL;
}
if($fileWrite) {
$fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
}
?>

</head>

<body>
<form id="some" name="someName" method="post">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<input type="submit" value="submit" class="submitClass"/>
</form>
</body>
</html>

I have worked around what @RamRider had pointed out earlier and added .PHP_EOL that enables new line break ( check manual for better understanding ). Use this code and let us know if there is any errors. I will try to help as much as i can.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM