如何使用Javascript将表单输入数据保存到XML文件?
我想将这些表单输入保存在一个文件(例如XML)中,以便以后在表单中再次使用:如何使用Javascript将表单输入数据保存到XML文件?,javascript,jquery,html,client-side-data,Javascript,Jquery,Html,Client Side Data,我想将这些表单输入保存在一个文件(例如XML)中,以便以后在表单中再次使用: <html> <body> <form action="demo_form.asp"> First name: <input type="text" name="FirstName" value="Mickey"><br> Last name: <input type="text"
<html>
<body>
<form action="demo_form.asp">
First name: <input type="text" name="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>
<p>Click the "Submit" button and the form-data will be sent to a page on the server called "demo_form.asp".</p>
</body>
</html>
名字:
姓氏:
单击“提交”按钮,表单数据将发送到服务器上名为“demo_form.asp”的页面
最简单的方法是什么?我不能使用PHP或ASP.NET 除非您使用服务器端语言将数据保存到文件系统(我假设您的意思是希望保存XML文件),否则不可能。JavaScript应用程序无法写入文件系统
您可以使用。请看“您所要求的只能在服务器端完成 从你的问题中,我可以理解你是网络开发的新手,我知道你只想保留数据,以便以后需要时使用
我的建议是将cookie、url或隐藏字段中的值保留为XML字符串。您最好选择隐藏字段。如果需要将数据保存到服务器,则需要服务器端工具ASP.NET或PHP,如您所说 但是,您说过希望以XML格式存储数据,以便以后在表单中使用。稍后您可以在同一页面中使用数据,并将其转换为XML,如下所示:
interval=setInterval(function(){
first=$('[name*="FirstName"]').val();
last=$('[name*="LastName"]').val();
output='<data><first>'+first+'</first><last>'+last+'</last></data>';
$('#output').text(output);
},200);
JS:
提交表单时,此javascript将触发表单数据的文件下载(针对每个表单)。它不使用XML,因为我不知道您的模式是什么样的(我怀疑它是否有用,而表单序列化字符串可以使用xhr快速发布)
将它构建到bookmarklet或其他任何东西中。但是,如果您希望保存一些输入,您可能希望将值存储在localStorage中,而不是存储在文件中。您没有提到您打算如何重用数据,从localStorage中提取数据要比构建动态文件上传器并让用户找到正确的文件容易得多。下面的代码示例将允许您从页面上的字段中获取数据,并将其直接写入用户本地框的txt文件。我目前正在寻找一种方法,为我正在进行的一个项目将它们写入XMl,但至少我可以得到我想要写入文本文件的数据
var userInput = document.getElementById("userInputId").value;
var fileURL = 'data:application/notepad;charset=utf-8,' + encodeURIComponent(userInput);
var fileName = "test.txt";
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if (!!window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
您可以很容易地将数据下载到XML文件中,但“以后使用”是什么意思?上面的代码在IE 10.X/11和firefox中无法正常工作。
firstName=document.form.FirstName.value;
lastName=document.form.LastName.value;
(function () {
var makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
return window.URL.createObjectURL(data);
},
serializeForm = function(form, evt){
var evt = evt || window.event;
evt.target = evt.target || evt.srcElement || null;
var field, query='';
if(typeof form == 'object' && form.nodeName == "FORM"){
for(i=form.elements.length-1; i>=0; i--){
field = form.elements[i];
if(field.name && field.type != 'file' && field.type != 'reset'){
if(field.type == 'select-multiple'){
for(j=form.elements[i].options.length-1; j>=0; j--){
if(field.options[j].selected){
query += '&' + field.name + "=" + encodeURIComponent(field.options[j].value).replace(/%20/g,'+');
}
}
}
else{
if((field.type != 'submit' && field.type != 'button') || evt.target == field){
if((field.type != 'checkbox' && field.type != 'radio') || field.checked){
query += '&' + field.name + "=" + encodeURIComponent(field.value).replace(/%20/g,'+');
}
}
}
}
}
}
return query.substr(1);
}
, _onsubmit = function() {
var _href = makeTextFile(serializeForm(this));
var _a = document.createElement("A");
_a.setAttribute('download', 'export.txt');
_a.setAttribute('target', '_blank');
_a.href = _href;
_a.click();
window.URL.revokeObjectURL(_href);
//return false;
};
[].forEach.call(
document.querySelectorAll('form'),
function(f) { f.onsubmit = _onsubmit; }
);
})();
var userInput = document.getElementById("userInputId").value;
var fileURL = 'data:application/notepad;charset=utf-8,' + encodeURIComponent(userInput);
var fileName = "test.txt";
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if (!!window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}