php - 如何在没有提交按钮的情况下将表单文本区域数据发送到 txt 文件?
问题描述
我希望访问者复制/粘贴或写入文本区域字段的数据保存到网络服务器中的 txt 文件中,但我似乎无法弄清楚。
到目前为止,我有这个 html:
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label for="data" class="col-sm-2 control-label">The Data</label>
<div class="col-sm-10">
<textarea id="data" onChange="saveToTxt(this); class="form-control"></textarea>
</div>
</div>
剧本:
<script>
function saveToTxt(fld) {
const textAreaValue = fld.value;
// then use textArea variable as container of textarea-content
// and then treat it as you want.
}
</script>
php文件写入txt文件:
<?php
$handle = fopen("data.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
我似乎无法弄清楚如何在无需访问者按下任何按钮的情况下从表单中调用 php。
有什么建议吗?
编辑:我也试过:
<script>
function editPhrase() {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("filename.txt", True);
var phraseChange = document.getElementById('data');
s.writeline("Phrase :" + phraseChange);
s.writeline("-----------------------------");
s.Close();
}
</script>
解决方案
尝试这个
`$('#data').on('keyup', function() {
var data = $('#data').val();
$.ajax({
url: './process.php',
method: 'POST',
data: {data: data}
})
});`
PHP
<?php
$text = $_POST['data'];
$handle = fopen("data.txt", "w");
fwrite($handle, $text);
fclose($handle);
?>
推荐阅读
- c - 循环遍历一个动态分配的结构数组并填充每个结构
- java - 在 Java Spring 中从流创建 CSV 文件输出
- swift - 错误:闭包内的“变量在其自己的初始值内使用”
- amazon-web-services - 当 AWS Elasticache Redis 集群向外扩展时,Terraform 计划失败
- python - 关于 Matplotlib 的基本问题 - 忽略第一个值和 - 让你的 x 值滚动而不是缩小
- linux - Docker Open VPN 已连接但无法在 docker 中访问 Internet
- ecmascript-6 - 在宿主环境中操作计算对象(ECMAScript 文档定义)
- php - PHP 没有在 Ubuntu 服务器上重定向
- python - Python:删除/复制文件。第一次运行成功,现在没有
- javascript - 根据数量更新价格 HTML, JS