2013-06-01 7 views
8

スクリプトを使用してhtmlフォームデータをtxtファイルに提出する方法があるかどうかは疑問でしたが、ウェブサーバー、ウェブホスト、 wamp、xampなど。ウェブサーバーを使用せずにtxtファイルにhtmlフォームデータを書き込む

私はPHPスクリプトを試してきましたが、投稿時にPHPドキュメントを開くだけです。

すべてのヘルプは大歓迎されています、ActiveX残念ながら

<script type = "text/javascript"> 
function WriteToFile(passForm) 
{ 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var s = fso.CreateTextFile("C:\\Test.txt", true); 
s.WriteLine(document.passForm.input.value); 
s.Close(); 
} 
</script> 

:これが機能しない場合

<script type ="text/javascript"> 
function WriteToFile(passForm) { 

    set fso = CreateObject("Scripting.FileSystemObject"); 
    set s = fso.CreateTextFile("C:\test.txt", True); 
    s.writeline(document.passForm.input1.value); 
    s.writeline(document.passForm.input2.value); 
    s.writeline(document.passForm.input3.value); 
    s.Close(); 
} 
    </script> 

、代替はActiveXオブジェクトである:D

+1

<!DOCTYPE html> <html> <head> <style> form * { display: block; margin: 10px; } </style> <script language="Javascript" > function download(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); pom.style.display = 'none'; document.body.appendChild(pom); pom.click(); document.body.removeChild(pom); } </script> </head> <body> <form onsubmit="download(this['name'].value, this['text'].value)"> <input type="text" name="name" value="test.txt"> <textarea rows=3 cols=50 name="text">PLEASE WRITE ANSWER HERE. </textarea> <input type="radio" name="radio" value="Option 1" onclick="getElementById('problem').value=this.value;"> Option 1<br> <input type="radio" name="radio" value="Option 2" onclick="getElementById('problem').value=this.value;"> Option 2<br> <form onsubmit="download(this['name'].value, this['text'].value)"> <input type="text" name="problem" id="problem"> <input type="submit" value="SAVE"> </form> </body> </html> 
可能重複 - :

コードは以下です。私はあなたがこれを行うことができるとは思わないが、Webサーバーをインストールするための助けが必要な場合は、それについてのクエリを投稿することもできます – tay10r

+1

[JavaScriptを使用してファイルを読み書きする方法](http:// stackoverflow .com/questions/585234/how-to-read-and-file-using-javascript) –

+0

私は私の答えで逃した何か:[クッキー](http://www.w3schools.com /js/js_cookies.asp)コンピュータにデータを保存するには – imulsion

答えて

8

あなたはJavaScriptを使用することができます私の知る限り、オブジェクトはIEでのみサポートされています。

+0

偉大な答え。好奇心のために、このクロスブラウザは互換性がありますか? – tay10r

+0

@TaylorFloresこれは主にJavaScriptの古いバージョンでも機能するためです。私はそれが一般的に動作すべきだと思います。 – imulsion

+0

よろしくお願いします。 – tay10r

5

これは何か?

<!DOCTYPE html> 
<html> 
<head> 
<style> 
form * { 
    display: block; 
    margin: 10px; 
} 
</style> 
<script language="Javascript" > 
function download(filename, text) { 
    var pom = document.createElement('a'); 
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 

encodeURIComponent(text)); 
    pom.setAttribute('download', filename); 

    pom.style.display = 'none'; 
    document.body.appendChild(pom); 

    pom.click(); 

    document.body.removeChild(pom); 
} 
</script> 
</head> 
<body> 

<form onsubmit="download(this['name'].value, this['text'].value)"> 
    <input type="text" name="name" value="test.txt"> 
    <textarea rows=3 cols=50 name="text">Please type in this box. When you 

click the Download button, the contents of this box will be downloaded to 

your machine at the location you specify. Pretty nifty. </textarea> 
    <input type="submit" value="Download"> 
</form> 
</body> 
</html> 
+0

これは私にとって完璧にうまくいっていて、私に多くの時間を救ってくれました。 (マイナーチェンジで)。投稿していただきありがとうございます。 – rh4games

0

私はラジオボタンのエントリを保存するには、このコードに少し変更を加えたが、ラジオボタンを選択した後、テキストボックスに表示されるテキストを保存することができません。 http://stackoverflow.com/questions/6051143/how-to-use-html-forms-without-a-serverの

関連する問題