当用户提交表单时,我正在尝试在服务器上创建.txt文件。然而
这是我的代码:
javascript
function sendInfo(){
var name = document.getElementById("scenarioName").value;
var author = document.getElementById("author").value;
var email = document.getElementById("email").value;
var comments = document.getElementById("comments").value;
var urlString = "get_info.php?name="+name+"&author="+author+"&email="+email+"&comments="+comments;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (this.readyState==4 && this.status==200)
{
document.getElementById("successParagraph").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",urlString,true);
xmlhttp.send();}
php
$content = $_GET['name']."rn".$_GET['author']."rn".$_GET['email']."rn".$_GET['comments'];
if(isset($_GET['name'])){
$fp = fopen("files/".$_GET['name'].".txt","wb");
fwrite($fp,$content);
fclose($fp);
}
因此,如果评论是:
" foo
bar"
然后在文本文件中,这将是一行,阅读" foobar"
对,我不知道为什么或如何,但是我已经为此做了解决方案。这个问题似乎是PHP文件编写内容的方式,它似乎忽略了新线。因此,我将评论按Newline字符分开,并使用foreach循环将每行写入文件中。因此:
$content = $_POST['name']."rn".$_POST['author']."rn".$_POST['email']."rn";
$comments = split("n",$_POST['comments']);
if(isset($_POST['name'])){
$fp = fopen("fine-uploader/files/".$_POST['name'].".txt","wb");
fwrite($fp,$content);
foreach($comments as $c){
fwrite($fp,$c);
fwrite($fp,"rn");
}
fclose($fp);
}