javascript 按下按钮后写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20947139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write to file upon button press
提问by amartin94
Trying to write to a txt file upon button click. Not sure how to do it and my code isnt working. Im starting to think i might need AJAX? If so, can someone give me a quick pointer in the right direction? Please and thankyou in advance!
尝试在单击按钮时写入 txt 文件。不知道该怎么做,我的代码不起作用。我开始认为我可能需要 AJAX?如果是这样,有人可以给我一个正确方向的快速指示吗?请提前谢谢!
Here's my code:
这是我的代码:
<?php
//write to file on button event
function buttonWrite()
{
$filename = 'button.txt';
@ $fp = fopen("messages/".$filename, 'wb');
if (!$fp)
{
echo '<p><strong>Cannot generate message file</strong></p></body></html>';
exit;
}
$outputstring = 'Hello, i have been generated by the button';
fwrite($fp, $outputstring);
}
?>
<html>
<head>
<title>Button Writer</title>
</head>
<body>
<button type="button" onclick="buttonWrite()">Write to File</button>
</body>
</html>
UPDATE! None of these answers actually work at all.. Not sure if its the server im hosting on or what, but yeah the just dont work.. any ideas guys ?
更新!这些答案都没有实际工作......不知道它是我托管的服务器还是什么,但是是的,只是不起作用......有任何想法吗?
回答by Zword
Create a PHP file.Lets say "abc.php".It contains:
创建一个PHP 文件。让我们说“abc.php”。它包含:
<?PHP
$filename = 'button.txt';
@ $fp = fopen("messages/".$filename, 'wb');
if (!$fp)
{
echo '<p><strong>Cannot generate message file</strong></p></body></html>';
exit;
}
else
{
$outputstring = 'Hello, i have been generated by the button';
fwrite($fp, $outputstring);
Echo "Message inserted";
}
?>
HTML file
HTML文件
<html>
<head>
<title>Button Writer</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
</head>
<body>
<button id="button1" type="button">Write to File</button>
</body>
<script type='text/javascript'>
$('#button1').click(function(){
$.ajax({
type: "POST",
url: "abc.php",
data: "",
success: function(msg){
alert(msg);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Some error occured");
}
});
});
</script>
</html>
回答by Ghermans41
Entire Code
完整代码
Save.php
保存.php
<?php
//write to file on button event
function buttonWrite()
{
$filename = 'button.txt';
$outputstring = "Hello, i have been generated by the button'\n";
file_put_contents($filename, $outputstring, FILE_APPEND | LOCK_EX);
}
?>
index.php or index.html
index.php 或 index.html
<html>
<head>
</head>
<body>
<form name="input" action="save.php">
<input type="submit" value="Write to File">
</form>
</html>
This should work it will write the $outputstring to the button.txt using the file_put_contents function
这应该可以工作,它将使用 file_put_contents 函数将 $outputstring 写入 button.txt
回答by tuffkid
You do it wrong - javascript and php can't be executed together in HTML. What you need to do is to create:
你做错了 - javascript 和 php 不能在 HTML 中一起执行。您需要做的是创建:
1) "onclick" button event, that execute AJAX request to your server. I'll use jQueryin my examples for the simplicity.
1) “onclick”按钮事件,执行 AJAX 请求到您的服务器。为简单起见,我将在示例中使用jQuery。
<button id="btn">SAVE FILE</button>
<script type="text/javascript">
// bind click event to button
$('#btn').click(function() {
// send ajax request to save.php (using POST method)
$.post('save.php', { text: "sample text", function(data) {
// output response to console
console.log(data);
});
});
</script>
2) create save.php file - it will be responsible for saving your data to the file
2) 创建 save.php 文件 - 它将负责将您的数据保存到文件中
<?php
// saving sample text to file (it doesn't include validation!)
file_put_contents('file.txt', $_POST['text']);
die('file has been saved');
?>