Javascript/JQuery将值保存到文件 [英] Javascript / JQuery Save value to file
问题描述
页面上有几个单选按钮,我需要保存它们的值或选择的任何值到文件中.
I have a few radio buttons in a page and I need to save their values or whatever value is selected to a file.
这是示例代码:
<input name="select" type="radio" value="a" />
<input name="select" type="radio" value="b" />
<input name="select" type="radio" value="c" />
<input name="select" type="radio" value="d" />
无论何时用户选择一个,我都需要将其保存/附加到文件中.
Whenever the user selects one I need it saved / appended into a file.
可能吗?
更新:
尝试过:
<script>
$(document).ready(function () {
$("input[name='select']").change(function(e) {
e.preventDefault();
$.post("zzz.html", { selectedValue : $(this).val() }, function(response) {
// do something with server response
alert('saved');
});
});
});
</script>
结果:无结果.没有创建任何内容/没有保存任何内容.
Result: No Result. Nothing created / Nothing Saved.
推荐答案
如果要将文本保存到服务器上的文件中,则可以使用php.
If you want to save the the text to a file on your server, then you can use php.
此示例检查以查看是否已设置文件名,还设置了要记录的文本. 它会打开文件,并通过指定"a +"将文本追加到文件中.
This example checks to see if a file name has been set, and the text to log has also been set. It opens the file and appends text to it by specifying "a+".
所以来访问
http://websitename.com/myfile.php?filename=test.txt&log=string to write
将在服务器上创建文本并将其附加到该文件. 如果您想将文本文件提供给用户下载,则可以使用
Will create and append the text to that file on your server. If then you want to provide the textfile to the user to download you can just use
http://websitename.com/test.txt
此代码未经测试
<?php
if(isset($_GET["filename"]) == true && isset($_GET["log"]) == true){
$fp = fopen($_GET["filename"], "a+");
if($fp !== null){
fputs($fp, $_GET["log"] . "\r\n");
fclose($fp);
}
}
?>
jquery的获取方法
jquery get method
$.get("myfile.php", { filename: "text.txt", log: "string to write" });
这篇关于Javascript/JQuery将值保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!