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.
Here is a sample code:
<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.
Possible?
UPDATE:
Tried this:
<script>
$(document).ready(function () {
$("input[name='select']").change(function(e) {
e.preventDefault();
$.post("zzz.html", { selectedV开发者_开发问答alue : $(this).val() }, function(response) {
// do something with server response
alert('saved');
});
});
});
</script>
Result: No Result. Nothing created / Nothing Saved.
$("button").click(function(){
$.post('save_values.php', $("form").serialize(), function(){
alert('saved');
});
return false;
});
If you want to save the the text to a file on your server, then you can use php.
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+".
So visiting
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
This code is untested
<?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 get method
$.get("myfile.php", { filename: "text.txt", log: "string to write" });
Assuming you want to save the selected value every time the radio group has been changed:
$("input[name='select']").change(function(e) {
e.preventDefault();
$.post("foo.html", { selectedValue : $(this).val() }, function(response) {
// do something with server response
});
});
If it is on server you can submit to a servlet and open a file and write on the file.
if it is in client side you can try using this jQuery plugin http://jquery.tiddlywiki.org/twFile.html and do that...
精彩评论