Reputation: 9
NEW SITUATION/QUESTION
This is the same question, but I am trying it a different way. If you see what I did below and my comments with Rob, I was trying to do it with PHP, but I've spent the past few days trying to get PHP on my nginx server through Docker, and I've been having a really tough time.
So, I decided to go back to the initial problem (the question in the title). So, I tried to implement some other code online and something happens, but I just have a few questions about it. With this code:
function textbox(){
document.getElementById("textInput").className="show";
}
function WriteToFile(passForm) {
var fso = CreateObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("/home/bsluss/b_test/CTO-Signing-Server/signing-server/www/cgi-bin/tmp.txt", True);
var textinfo = document.getElementById('message');
s.writeline(textinfo);
s.close();
}
</script>
...
<input type="button" value="Add Cloud Info" onclick="textbox()" />
<div id="textInput" class="hide">
<form id="form" class="topBefore" onSubmit="WriteToFile(this)">
<textarea type="text" name="text" id="message" value=""></textarea><br>
<input id="submit" type ="submit" value="Copy to File"></input>
</form>
</div>
</body>
</html>
After typing in '1234' in the textarea and upon submit, it refreshed my page with the same url, but 'text=1234' was on the end. I'm just not sure where this txt document that I wanted to create is. It's not in the filepath that I specified in the WriteToFile function. I'm not sure if this is because I'm doing this on Docker, a virtual host server? Any help regarding this would be great!
OLD SITUATION/QUESTION
I have this server from an IP address. I have other buttons and such that will take me to other pages. I am trying to implement a textarea that will allow the user to paste text and then on a button click or 'submit' click will copy that text to a tmp txt file which I will run some pre-made scripts on. I'm having a really hard time figuring out not only how to pass this text to a new txt file, but also accessing it through the server (through PUTTY via the IP address). I believe I need to use PHP to do this, which I haven't used until today. Any help would be greatly appreciated. Here's what I have so far. This is all in the same index.html file. I tried creating a filename.php file and letting the action=filename.php, but was getting similar results and don't really understand the difference.
<?php
if(isset($_POST["text"])) {
$txt = $_POST["text"];
$file = $_SERVER['DOCUMENT_ROOT'].'/tmp/tmpdata.txt';
$fp = fopen($file, "a+");
fwrite($fp, $txt . PHP_EOL);
fclose($fp);
echo 'Success.';
} else{
echo 'Error.';
}
?>
<html>
<body>
<form id="form" class="topBefore" action="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>" method="POST">
<textarea type="text" name="text" id="message" value=""></textarea><br>
<input id="submit" type ="submit" value="Copy to File"></input>
</form>
</body>
</html>
Upon clicking the submit button with this code, it brings me to a new page with error code 404 Page not Found. If I change the method to GET instead of POST, then it will download a txt file using the Notepad application (which is closer to what I want), but I cannot find this new file under the file tree on the server.
Upvotes: 0
Views: 545
Reputation: 3707
The file extension should probly be .php, not .html, unless your server is configured to parse HTML as PHP, which I would recommend against. The action of the form can be empty, since you are submitting to the same file, so action="" as @LawrenceCherone mentioned in his comment.
Unless you really need the text file to be publicly available, best practice is to store it somewhere outside the document root. Since you mentioned accessing it via PUTTY, it sounds like that won't be a problem. Your code works, but I recommend adding some error checking at a minimum. The HTML also has some quirks I recommend ironing out. Here's a solid starting point.
<?php
// Set up a status flag end message string
$status = 'init';
$msg = '';
// If the submit button was pressed, attempt to process the post
if (isset($_POST["submit"]))
{
try
{
// Make sure we have text, if not, return an error
if (empty($_POST["text"]))
{
throw new Exception('Please enter text');
}
$txt = $_POST["text"];
/*
* Path to the file - it's a good practice to place this outside the
* document root so it's not publicly available or executable by the
* server.
*/
$file = $_SERVER['DOCUMENT_ROOT'] . '/tmp/tmpdata.txt';
// Attempt to open a file handle, If we can't, bail out with an error
if (($fp = fopen($file, "a+")) === false)
{
throw new Exception('Unable to open file for writing');
}
if(fwrite($fp, $txt . PHP_EOL) === false)
{
throw new Exception('Unable to write to file');
}
fclose($fp);
// Set the status to success
$status = 'success';
}
catch(Exception $e)
{
$status = 'error';
$msg = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php if ($status == 'error') { ?>
<div class="alert alert-error"><?php echo $msg; ?></div>
<?php } elseif ($status == 'success') { ?>
<div class="alert alert-success">Text stored successfully</div>
<?php } ?>
<body>
<form id="form" class="topBefore" action="" method="POST">
<textarea name="text" id="message"></textarea><br>
<input name="submit" type="submit" value="Copy to File">
</form>
</body>
</html>
Upvotes: 1